一、异常概述与异常体系结构
1.异常概述
1.1异常的定义
1.2异常的分类
1.3说明
捕获异常最好的时间是在编译期,但有些异常是在运行期才能发现。比如:数组角标越界、除数为0的情况。
2.异常体系结构
* java.lang.Throwable * |-----java.lang.Error:一般不编写针对性的代码进行处理。 * |-----java.lang.Exception:可以进行异常的处理 * |------编译时异常(checked) * |-----IOException * |-----FileNotFoundException * |-----ClassNotFoundException * |------运行时异常(unchecked,RuntimeException) * |-----NullPointerException * |-----ArrayIndexOutOfBoundsException * |-----ClassCastException * |-----NumberFormatException * |-----InputMismatchException * |-----ArithmeticException

二、常见异常
1.运行时异常
public class test {
public static void main(String[] args) {
String[] arr = new String[]{"str1", "str2", "str3"};
System.out.println(arr[3]);
}
}
public class test {
public static void main(String[] args) {
Person p = new Person();
p = null;
System.out.println(p.name);//NullPointerException
}
}
class Person{
String name;
int age;
}
public class test {
public static void main(String[] args) {
int a = 10;
int b = 0;
System.out.println(a / b);//ArithmeticException: / by zero
}
}
public class test {
public static void main(String[] args) {
Object obj = new Date();
test t = (test)obj;
System.out.println(t);//ClassCastException
}
}
NumberFormatException:
public class test {
public static void main(String[] args) {
String s = "abc";
int i = Integer.parseInt(s);
System.out.println(i);//NumberFormatException
}
}
InputMismatchException:
public class test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int score = scan.nextInt();
//如果在控制台输入的不是int型的,会报异常
System.out.println(score);
scan.close();
}
}
2.编译时异常
@Test
public void test7(){
// File file = new File("hello.txt");
// FileInputStream fis = new FileInputStream(file);
//
// int data = fis.read();
// while(data != -1){
// System.out.print((char)data);
// data = fis.read();
// }
//
// fis.close();
}
三、异常处理
1.抓抛模型
过程一:“抛”
程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象,并将此对象抛出。一旦抛出对象以后,其后的代码就不再执行。
异常对象的产生:
> 系统自动生成的异常对象
> 手动的生成一个异常对象,并抛出(throw)
过程二:“抓”
可以理解为异常的处理方式:① try-catch-finally ② throws
2.异常处理的两种方式
2.1方式一:try-catch-finally
格式:
try{
可能出现异常的代码;
}catch(异常类型1 变量1){
异常处理的方式一;
}
catch(异常类型2 变量2){
异常处理的方式二;
}
catch(异常类型3 变量3){
异常处理的方式三;
}
...
finally{
一定会被执行的代码;
}
public class test {
public static void main(String[] args) {
String[] arr = new String[]{"str1", "str2", "str3"};
try {
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("处理完成");
}
}
}
说明:
①finally是可选的,其中的代码一定会被执行;
②catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的上面。否则,报错;
③在try结构中声明的变量,再出了try结构以后,就不能再被调用
④try-catch-finally结构可以嵌套
⑤常用的异常对象处理的方式: ① String getMessage() ② printStackTrace()
2.2方式二:throws + 异常类型

public class test {
public static void main(String[] args) {
test t = new test();
try {
t.readFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public void readFile() throws IOException {
FileInputStream in = new FileInputStream("atguigushk.txt");
int b; b = in.read();
while (b != -1) {
System.out.print((char) b);
b = in.read();
}
in.close();
}
}
四、手动抛出异常
面试题:throw和throws的区别?
throw:表示抛出一个异常类的对象,生成异常对象的过程。声明在方法体内。
throws:属于异常处理的一种方式,声明在方法的声明处。
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.继承于现有的异常类:RuntimeException、Exception
2.提供全局常量:serialVersionUID
3.提供重载的构造器;
class Student{
private int id;
public void regist(int id) throws Exception {
if (id > 0){
this.id = id;
}else{
throw new MyException("您输入的数据非法");
}
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
'}';
}
}
class MyException extends Exception{
static final long serialVersionUID = -338751693453229948L;
public MyException(){
}
public MyException(String msg){
super(msg);
}
}
作者:Java之美
日期:2020-03-30
来源:https://www.cnblogs.com/897463196-a/p/12599333.html