您好,欢迎访问一九零五行业门户网

java异常如何捕获与处理

java异常捕获与处理的方法:1、throws关键字,由系统自动将所捕获的异常信息抛给上级调用方法;2、throw的作用是手工抛出异常类的实例化对象;3、runexception是exception的子类,由用户选择是否进行处理。
【相关学习推荐:java基础教程】
java异常捕获与处理的方法:
一、异常处理的使用
由于finally块是可以省略的,异常处理格式可以分为三类:try{ }——catch{ }、try{ }——catch{ }——finally{ }、try{ }——finally{ }。
1 public class dealexception 2 { 3 public static void main(string args[]) 4 { 5 try 6 //要检查的程序语句 7 { 8 int a[] = new int[5]; 9 a[10] = 7;//出现异常10 }11 catch(arrayindexoutofboundsexception ex)12 //异常发生时的处理语句13 {14 system.out.println("超出数组范围!");15 }16 finally17 //这个代码块一定会被执行18 {19 system.out.println("*****");20 }21 system.out.println("异常处理结束!");22 }23 }
可以看出,在异常捕捉的过程中要进行两个判断,第一是try程序块是否有异常产生,第二是产生的异常是否和catch()括号内想要捕捉的异常相同。
那么,如果出现的异常和catch()内想要捕捉的异常不相同时怎么办呢?事实上我们可以在一个try语句后跟上多个异常处理catch语句,来处理多种不同类型的异常。
1 public class dealexception 2 { 3 public static void main(string args[]) 4 { 5 try 6 //要检查的程序语句 7 { 8 int a[] = new int[5]; 9 a[0] = 3;10 a[1] = 1;11 //a[1] = 0;//除数为0异常12 //a[10] = 7;//数组下标越界异常13 int result = a[0]/a[1];14 system.out.println(result);15 }16 catch(arrayindexoutofboundsexception ex)17 //异常发生时的处理语句18 {19 system.out.println("数组越界异常");20 ex.printstacktrace();//显示异常的堆栈跟踪信息21 }22 catch(arithmeticexception ex)23 {24 system.out.println("算术运算异常");25 ex.printstacktrace();26 }27 finally28 //这个代码块一定会被执行29 {30 system.out.println("finally语句不论是否有异常都会被执行。");31 }32 system.out.println("异常处理结束!");33 }34 }
上述例子中ex.printstacktrace();就是对异常类对象ex的使用,输出了详细的异常堆栈跟踪信息,包括异常的类型,异常发生在哪个包、哪个类、哪个方法以及异常发生的行号。
二、throws关键字
throws声明的方法表示该方法不处理异常,而由系统自动将所捕获的异常信息抛给上级调用方法。
1 public class throwsdemo 2 { 3 public static void main(string[] args) 4 { 5 int[] a = new int[5]; 6 try 7 { 8 setzero(a,10); 9 }10 catch(arrayindexoutofboundsexception ex)11 {12 system.out.println("数组越界错误!");13 system.out.println("异常:"+ex);14 }15 system.out.println("main()方法结束。");16 }17 private static void setzero(int[] a,int index) throws arrayindexoutofboundsexception18 {19 a[index] = 0;20 }21 }
throws关键字抛出异常,“arrayindexoutofboundsexception”表明setzero()方法可能存在的异常类型,一旦方法出现异常,setzero()方法自己并不处理,而是将异常提交给它的上级调用者main()方法。
三、throw关键字
throw的作用是手工抛出异常类的实例化对象。
1 public class throwdemo 2 { 3 public static void main(string[] args) 4 { 5 try 6 { 7 //抛出异常的实例化对象 8 throw new arrayindexoutofboundsexception("\n个性化异常信息:\n数组下标越界"); 9 }10 catch(arrayindexoutofboundsexception ex)11 {12 system.out.println(ex);13 }14 }15 }
我们能发现,throw好像属于没事找事,引发运行期异常,并自定义提示信息。事实上,throw通常和throws联合使用,抛出的是程序中已经产生的异常类实例。
exceptiondemo
输出结果:
setzero方法开始:
setzero方法结束。
异常:java.lang.arrayindexoutofboundsexception: 10
main()方法结束!
四、runtimeexception类
exception和runtimeexception的区别:
exception:强制性要求用户必须处理;
runexception:是exception的子类,由用户选择是否进行处理。
五、自定义异常类
自定义异常类继承自exception类,可以使用父类的大量的方法,也可自己编写方法来处理特定的事件。java提供用继承的方式运行用户自己编写的异常类。
1 class myexception extends exception 2 { 3 public myexception(string message) 4 { 5 super(message); 6 } 7 } 8 public class definedexception 9 {10 public static void main(string[] args) 11 {12 try13 {14 throw new myexception("\n自定义异常类!");15 }16 catch(myexception e)17 {18 system.out.println(e);19 }20 }21 }
相关学习推荐:编程视频
以上就是java异常如何捕获与处理的详细内容。
其它类似信息

推荐信息