若是是以Exception结尾的,那么确定就是一个异常。html
异常的处理:
方式一:捕获处理(往下面还有方式二)
捕获处理的格式:
try{
可能发生异常的代码;
}catch(捕获的异常类型 变量名){
处理异常的代码....
}
java
捕获处理要注意的细节:
1.code1中体现→若是try块中代码出了异常通过了处理以后,那么try-catch块外面的代码能够正常执行。
2.code2中体现→若是try块中出了异常的代码,那么在try块中出现异常代码后面的代码是不会执行了。
3.code3中体现→一个try块后面是能够跟有多个catch块的,也就是一个try块能够捕获多种异常的类型。
4.code4中体现→一个try块能够捕获多种异常的类型,可是捕获的异常类型必须从小到大进行捕获,不然编译报错。jvm
code1spa
package day10; public class TestException2 { public static void main(String[] args){ test(1,0); } public static void test(int a , int b){ try{ int c = a/b; }catch (ArithmeticException e){ System.out.println("异常处理了 并返回当前异常对象的完整类名+病态信息 :"+e.toString()); } System.out.println("try-catch块外面的代码能够正常执行中"); } }运行结果:
异常处理了 回当前异常对象的完整类名+病态信息 :java.lang.ArithmeticException: / by zero
try-catch块外面的代码能够正常执行中
指针
code2code
package day10; public class TestException2 { public static void main(String[] args){ test(1,0); } public static void test(int a , int b){ try{ int c = a/b; System.out.println("看看是否被执行到~"); }catch (ArithmeticException e){ System.out.println("异常处理了 并返回当前异常对象的完整类名+病态信息 :"+e.toString()); } } }运行结果:
异常处理了 并返回回当前异常对象的完整类名+病态信息 :java.lang.ArithmeticException: / by zero
htm
”看看是否被执行到~“ 这句没有被执行到,由于int c = a/b;这句出现异常了,因此try块内的其他代码都不会执行,直接会被catch 处理掉。对象
code3it
public class TestException2 { public static void main(String[] args){ int[] arr = null; test(10,2,arr); } public static void test(int a , int b, int[] arr) { int c = 0 ; try{ c = a/b; System.out.println(arr.length); }catch (ArithmeticException e){ System.out.println("异常处理了 并返回回当前异常对象的完整类名+病态信息 :"+e.toString()); }catch (NullPointerException e){ System.out.println("出现了空指针异常...."); }catch (Exception e){ System.out.println("cc"); } System.out.println("c="+c); } }
运行结果:io
出现了空指针异常....
c=5
code3这段代码要注意,try块能检测多种异常,可是只能处理一种异常,就是第一个异常。
code4
public class TestException2 { public static void main(String[] args){ int[] arr = null; test(10,2,arr); } public static void test(int a , int b, int[] arr) { int c = 0 ; try{ c = a/b; System.out.println(arr.length); }catch (Exception e){ System.out.println("Exception类来捕获的话,其余类就捕获不了,至关于父类把工做都作完了,还有子类的事吗?"); }catch (NullPointerException e){ System.out.println("出现了空指针异常...."); }catch (ArithmeticException e){ System.out.println("cc"); } System.out.println("c="+c); } }
运行结果:
Exception 把 NullPointerException 和 ArithmeticException 的异常都捕获处理了,因此会提示错误,必须从小到大进行捕获,不然编译报错
方式二:异常的处理方式----抛出处理
抛出处理要注意的细节:
1.code5中体现→ 若是一个方法的内部抛出了一个异常 对象,那么必需要在方法上声明抛出。
2.code6中体现→若是调用了一个声明抛出异常 的方法,那么调用者必需要处理异常。
3.code7中体现→若是一个方法内部抛出了一个异常对象,那么throw语句后面的代码都不会再执行了(一个方法遇到了throw关键字,该方法也会立刻中止执行的)。
4.code8中体现→在一种状况下,只能抛出一种类型异常对象。(意思就是if())
throw 与throws两个关键字:
1. throw关键字是用于方法内部的,throws是用于方法声声明上的。
2. throw关键字是用于方法内部抛出一个异常对象的,throws关键字是用于在方法声明上声明抛出异常类型的。
3. throw关键字后面只能有一个异常对象,throws后面一次能够声明抛出多种类型的 异常
概念
※Exception在程序中必须使用try...catch进行处理。
※RuntimeException能够不使用try...catch进行处理,可是若是有异常产生,则异常将由JVM进行处理(默认往上抛)
code5
public class TestException2 { public static void main(String[] args){ try{ int[] arr = null; test(10,2,arr); }catch(NullPointerException e){ System.out.println("报告! 有异常出现"); e.printStackTrace(); } } public static void test(int a , int b, int[] arr) throws ArithmeticException,NullPointerException{ int c = 0 ; if(b==0){ throw new ArithmeticException(); //抛出一个异常对象... }else if(arr==null){ throw new NullPointerException(); } c = a/b; System.out.println(arr.length); System.out.println("c="+c); } }
运行结果:
报告! 有异常出现
java.lang.NullPointerException
at day10.TestException2.test(TestException2.java:22)
at day10.TestException2.main(TestException2.java:10)
code6
public class TestException2 { public static void main(String[] args) { //try{ int[] arr = null; test(4,0,arr); //调用了一个 声明抛出异常类型 的方法 //}catch(Exception e){ System.out.println("报告! 异常出现了"); // e.printStackTrace(); //} } public static void test(int a , int b, int[] arr) throws Exception,NullPointerException{ int c = 0 ; if(b==0){ throw new Exception(); //抛出一个异常对象... }else if(arr==null){ throw new NullPointerException(); } c = a/b; System.out.println(arr.length); System.out.println("c="+c); } }
运行结果:
P.S.
这code6中的代码 ,必定要注意 在test方法抛出的若是是RuntimeException类的话 ,就是不捕获也不会提示报错的,由于异常分为检查性异常和运行时异常 检查性异常须要捕捉,运行时异常不会强制要求捕捉,也就是说非RuntimeException 包括Eception 都是须要捕获
code7
public class TestException2 { public static void main(String[] args) { try{ int[] arr = null; test(4,0,arr); //调用了一个 声明抛出异常类型 的方法 }catch(Exception e){ System.out.println("报告! 异常出现了"); e.printStackTrace(); } } public static void test(int a , int b, int[] arr) throws Exception,NullPointerException{ int c = 0 ; if(b==0){ throw new Exception(); //抛出一个异常对象... }else if(arr==null){ throw new NullPointerException(); } c = a/b; System.out.println(arr.length); System.out.println("c="+c); } }运行结果:
报告! 异常出现了
java.lang.Exception
at day10.TestException2.test(TestException2.java:21)
at day10.TestException2.main(TestException2.java:11)
可见C的值没有打印出来,因此一抛出异常 后面的代码都不执行了。
code8
class Demo11 { public static void main(String[] args) { try{ int[] arr = null; div(10,0,arr); //调用了一个 声明抛出异常类型 的方法 }catch(Exception e){ System.out.println("报告! 出现异常了"); e.printStackTrace(); } } public static void div(int a, int b,int[] arr) throws Exception,NullPointerException { if(b==0 || arr==null){ throw new Exception(); //抛出一个异常对象... throw new NullPointerException(); } int c = a/b; System.out.println("c="+c); } }运行结果:
由于抛出了异常 就不会处理下面的语句了,方法都结束了 ,因此只能将2个异常分开成两种状况 才能避免编译时错误。
if(b==0){ throw new Exception(); //抛出一个异常对象... }else if(arr==null){ throw new NullPointerException(); }
交流企鹅:654249738,和自学者交流群:517284938