java中的异常概述
内容摘要
一、什么是异常异常就是程序运行过程中所出现的不正常现象。try:把可能发生异常的代码包起来,当发生异常时,将异常抛出catch:捕获异常并处理finally:不管是否发生异常,都会执行thr
文章正文
一、什么是异常
异常就是程序运行过程中所出现的不正常现象。
try
:把可能发生异常的代码包起来,当发生异常时,将异常抛出
catch
:捕获异常并处理
finally
:不管是否发生异常,都会执行
throw
:手动引发一个异常
throws
:定义任何被调用方法的异常
在线学习视频推荐:java在线视频
二、异常出现的原因
用户输入错误;
代码的错误;
环境的因素;
异常机制保证了程序的健壮性!
三、异常的分类
NullPointerException
空引用异常
IndexOutOfBoundException
下标越界异常
Error与编``译环境有关,它是Java运行时的内部错误以及资源耗尽错误。很难修复,不期望用户能修复。
四、获取异常信息
程序发生异常的时候,程序就直接从try执行到catch语句块,不再继续执行`在这里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class text3 { public static void main(String[] args) { System.out.println( "main开始执行" ); text3 a= new text3(); a.text(); System.out.println( "main执行结束" ); } public void text() { int a; try { a=2/0; System.out.println(a); } catch (ArithmeticException e){ System.out.println( "程序发生了异常" ); } } } |
异常捕获之后程序才不会断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class text3 { public static void main(String[] args) { System.out.println( "main开始执行" ); text3 a= new text3(); a.text(); System.out.println( "main执行结束" ); } public void text() { int a; //try { a=2/0; System.out.println(a); //}catch(ArithmeticException e){ //System.out.println("程序发生了异常"); //} } } |
控制台结果:
异常自己不处理但是得声明一下。
异常声明:指一个方法不处理它所产生的异常,而是调用层次向上传递,谁调用的这个方法谁来处理。
五、手动抛出异常
throw exception; 参数exception表示要抛出的异常对象,该对象是throwable类的子类,而且只能够是一个。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public void text1() throws ArithmeticException{ } public static void main(String[] args) { Text t= new Text(); try { t.text(); } catch (Exception e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); } } public void text() throws Exception { //手动抛出异常 throw new Exception( "这是手动抛出来的" ); } } |
六、try catch finally的嵌套使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class Textthrow { public static void main(String[] args) { double a=Math.random(); try { if (a>0.5) { System.out.println(a+ "程序不报错" ); } else { throw new Exception(); } } catch (Exception e) { System.out.println( "这是外层捕获的对象" +e); try { a=1/0; } catch (ArithmeticException e1) { System.out.println( "这是内层捕获的对象" +e1); }finally { System.out.println( "这是内层的finally块" ); } }finally { System.out.println( "这是外层的finally块 " ); } } } |
七、异常链
定义:两个或者多个不同的异常出现在同一个程序中,并且会发生嵌套抛出,我们称之为异常链。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public class ExceptionChainText { public static void main(String[] args) { Calculator c= new Calculator(); try { c.chufa(1, 0); } catch (NumberCalculateExcetption e) { e.printStackTrace(); System.out.println( "错误原因" +e); } } } class Calculator{ public int chufa(int i,int j) throws NumberCalculateExcetption { if (j==0) { NumberCalculateExcetption e = new NumberCalculateExcetption ( "计算错误" ); NegativeNumberException e1= new NegativeNumberException( "除数不能为0" ); e.initCause(e1); //由e1引起的异常 throw e; } return 0; } } class NegativeNumberException extends Exception{ public NegativeNumberException(String msg) { super(msg); } } class NumberCalculateExcetption extends Exception{ public NumberCalculateExcetption(String msg) { super(msg); } } |
代码注释
[!--zhushi--]