JAVA中的异常处理

xiaoxiao2021-02-28  123

异常和错误的概念以及区别

异常的定义 异常是不正常的事件,不是错误。异常是指程序运行的过程中,发生某些意外的事情,比如, 10处于0,0不能当除数,所有这样的文件是不存在等。下面我来举个”栗子”:public class ExcptionTest{ public static void main(String[] args) { System.out.println(“====运行前====”); int a=10/0; System.out.println(“====运行后====”); }

}

} 运行结果 Exception in thread “main” java.lang.ArithmeticException: / by zero at com.ExcptionTest.main(ExcptionTest.java:6) ====运行前====错误的定义 错误是很难处理的,比如内存溢出,不能够通过异常处理来解决。

异常的体系

Throwable类有两个直接子类:Exception类、Error类。 Exception表示异常,是所有异常类的父类,是程序员所关心的。Error表示错误,可能是编译期错误或者系统错误,往往程序中并不处理。请看下面的图片了解它的关系 也就是说exception的分类:RuntimeException 和CheckedException而RuntimeException也有4个子类它们分别是: ClassCastExceptionNullPointerExceptionArrayIndexOutOfBoundsExceptionArithmeticExceptionCheckedException也有2个子类: IOExceptionSqlException

异常详解

RuntimeException和CheckedException的区别 RuntimeException运行时异常,不会引起编译错误,不需要强制处理。CheckedException会引起编译错误,需要强制处理。

try-catch-finally的作用

try : 检测不安全的代码块(发现异常)catch: 异常处理代码finally:不管出现或者不出现异常都会执行的代码其中catch和finally可以省略,但是try不能可以多个catch块catch的异常,父类异常的catch块在子类异常之后可以catch任何异常catch CheckedException时,try块中必须存在可能引发该异常的代码,否则编译错误如果catch或者finally块中存在异常,则需要再次处理下面我来举个”栗子”:

public class Extceptions { public static void main(String[] args) { try{

int a=10/0; //现在try存在异常,它会直接跳到catch中 System.out.println("我在try");

}catch(ArithmeticException e){ System.out.println(“我在catch”); }finally{ //finally不管出现或者不出现异常都会执行的代码 System.out.println(“我在final”); }

}

}

输出结果 我在catch我在final

finally&return

是否 出现异常都会执行finally是否在正常代码和异常处理代码中return,仍然会先执行finally再return不会执行finally的情况:System.exit(0);我来举个”栗子”:

public class Extceptions { public static void main(String[] args) { try{ int b = 10/0;

System.out.println("try,try"); return;

}catch(ArithmeticException e){

System.out.println("catch catch");

// System.exit(0); 这个是结束虚拟机,所有就不会运行到finally这一步了 return; }finally{ System.out.println(“finally”); return;

} }

}

输出结果 catch catchfinally

层层抛出异常

throw 关键字throw用于抛出异常抛除异常后处理 使用try-catch捕获异常使用throws声明异常语法 throw new 异常类构造方法;throw new Exception();throws 用于方法上,指出方法引发的异常。可以声明多种异常类型,用逗号分开即可。

示例

public void test throws 异常1,异常2,异常3{

}

所谓层层抛出异常

就是 catch中,再次用throw抛出异常

举”栗子”

public class Throw1 { public static void main(String[] args) { try{ bang(130); }catch(Exception e){ e.printStackTrace();

} } private static void bang(int x)throws IOException{ big(x); } private static void big(int a)throws IOException{ if(a>120){ throw new IllegalArgumentException(“参数异常,年龄最大不超过120”); }

throw new IOException(“”);

}

}

总结

throw用于方法体中,用来抛出一个实际的异常对象,使用throw后,要么使用try catch捕获异常,要么使用throws声明异常throws用于方法声明处,用来声明该方法可能发生的异常类型,可以是多个异常类型,用来强制调用该方法时处理这些异常抽象方法也可以使用throws,所以说并不是有throw才有throws如果使用throw关键字抛异常,一定不要使用Exception,不能很好标记异常类型如果throw要抛出与业务逻辑有关的异常,需要使用自定义异常类

输出结果

java.lang.IllegalArgumentException: 参数异常,年龄最大不超过120 at Exception2.Throw1.big(Throw1.java:19) at Exception2.Throw1.bang(Throw1.java:15) at Exception2.Throw1.main(Throw1.java:8)

自定义异常

自定义异常就是自己定义的异常类,也就是Exception直接或间接的子类自定义异常类 public class 异常类名 extends Exception{ public 异常类名(String msg){ super(msg); } }

自定义异常类中往往不写其他方法,只重载需要使用的构造方法

自定义异常示例

public class BusinessException extends IOException{

public BusinessException() { super(); } public BusinessException(String message, Throwable cause) { super(message, cause);

} }

在程序中使用自定义异常大致可以分为一下几步

创建自定义异常类在方法中通过throw 关键字抛出自定义异常如果在当前抛出异常的方法中处理异常,可以使用try-catch语句捕获并处理,否则在方法的声明处通过throws关键字声明该异常调用throws声明该异常的方法时,使用try catch捕获自定义的异常类型,并在catch中进行处理
转载请注明原文地址: https://www.6miu.com/read-60747.html

最新回复(0)