Java基础-异常处理

1.1 异常概述与异常体系结构

  • 在Java语言中,将程序执行中发生的不正常情况称为“异常”
  • 分类:编译时异常和运行时异常
    异常
    异常

1.2 常见异常

package demo;

import java.util.Date;
import java.util.Scanner;
import org.junit.Test;
/*
 * java.lang.Throwable
 *      |----java.lang.Error
 *      |----java.lang.Exception
 *           |----编译时异常(checked)
 *                |----IOException
 *                    |-----FileNotFoundException
 *                |----ClassNotFoundException
 *           |----运行时异常(unchecked,RunimeException)
 *               |----NullPointerException
 *               |----ArrayIndexOutOfBoundsException
 *               |----ClassCastException
 *               |----NumberFormatException
 *               |----InputMismatchException
 *               |----ArithmeticException
 */
public class ExceptionTest {

	@Test
	public void test1() {
		int[] arr = null;
		System.out.println(arr[2]);

		String str = "abc";
		System.out.println(str.charAt(5));

		Object o = new Date();
		String str1 = (String) o;

		String str2 = "abc";
		Integer.parseInt(str);

		int i = 10 / 0;

		Scanner scanner = new Scanner(System.in);
		int nextInt = scanner.nextInt();

		int[] arr1 = new int[10];
		System.out.println(arr1[20]);
	}
}

1.3 异常处理机制一:try-catch-finally

  • Java提供的是异常处理的抓抛模型
  • Java的执行过程中如出现异常,会生成一个异常类对象,该异常对象将被提交给Java运行时系统,这个过程称为抛出(thorw)异常
package demo;

import org.junit.Test;

/*
 * 异常处理
 *
 * 过程一:“抛”,一旦抛出对象后,其后的代码就不再执行
 * 
 * 过程二:“抓”,①try-catch-finally ②throws
 */

public class ExceptionTest1 {
	@Test
	public void test1() {
		String str = "123";
		str = "abc";
		try {
			int num = Integer.parseInt(str);
			System.out.println("hello-1");
		} catch (NumberFormatException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
			System.out.println("出现异常了");
		}
		System.out.println("hello-2");
	}
}

package demo;

import org.junit.Test;

/*
 * finally的使用,可选的
 * finally中声明的是一定会被执行的代码,即使catch中又出现了异常,try中有return语句等情况
 * 像数据库、IO流,Socket等资源释放,需要放在finally中
 */
public class FinallyTest {
	@Test
	public void test1() {
		int a = 10;
		int b = 0;
		try {
			System.out.println(a / b);
		} catch (Exception e) {
			e.printStackTrace();

			int[] arr = new int[10];
			System.out.println(arr[10]);
		} finally {
			System.out.println("hello");
		}
	}

}

1.4 异常处理机制二:throws

package demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
 * 异常处理方式二:throws + 异常类型
 * 父类中被重写的方法没有throws处理异常,子类重写的方法页不能使用throws
 */
public class ExceptionTest2 {

	public void method1() throws IOException {
		File file = new File("hello.txt");
		FileInputStream fis = new FileInputStream(file);

		int read = fis.read();
		while (read != -1) {
			System.out.println(read);
			read = fis.read();
		}
	}
}

1.5 手动抛出异常:throw

package demo;

public class StudentTest {

	public static void main(String[] args) {
		try {
			Student student = new Student();
			student.regist(-1001);
			System.out.println(student);
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
}

class Student {
	private int id;

	public void regist(int id) throws Exception {
		if (id > 0) {
			this.id = id;
		} else {
			throw new Exception("输入的数据非法");
		}
	}

	@Override
	public String toString() {
		return "Student [id=" + id + "]";
	}

}

1.6 用户自定义异常类

package demo;
/*
 * 自定义异常类
 * 1.继承于现有的异常类RuntimeException
 */
public class MyException extends RuntimeException{

	static final long serialVersionUID = -70348975766939L;
	
	public MyException() {
		
	}
	
	public MyException(String msg) {
		super(msg);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值