package c;
/*异常处理第二部分
* 1 语句格式
* try{
* 语句组1
* }
* catch(异常类1 对象名)
* {
* 语句组2
* }
* catch(异常类2 对象名)
* {
* 语句组3
* }
* ...
* finally{
* 语句组4
}
注:catch语句块的异常对象封装了异常事件发生的信息,在catch语句块中可以使用异常对象的方法
来获得这些信息;
getMessage():返回异常信息
printStackTrace():用来跟踪异常事件发生时执行堆栈的内容
finally语句块为异常处理提供了一个统一的出口,使得在控制流程转到其他部分以前,能够对
程序的状态做统一的管理。通常在finally语句块中进行资源清除工作,如关闭打开的文件,清除临时
文件。无论try文件是否抛出异常,finally语句块总要执行,finally语句不被执行的唯一情况是程序
先执行了System.exit()。如果catch代码块和finally代码块并存,finally代码块必须放在catch代码块后面。
*/
/*
public class Exceptions1 {
public static void main(String args[]){
int x[]={4,2,0};
try{
System.out.println("商1:"+x[0]/x[1]);
x[3]=100;
System.out.println("商2:"+x[1]/x[2]);
}catch(ArithmeticException e)
{System.out.print("一");
}
catch(RuntimeException e){
System.out.println("二");
}
catch(Exception e){
System.out.println("三");
}
finally{
System.out.println("四");
}
System.out.println("五");
}
}
商1:2
二
四
五
*/
//未捕获异常
/*
public class Exceptions1{
public static void main(String args[]){
int x[]={4,2,0};
try{
System.out.println("商1:"+x[0]/x[1]);
x[3]=100;
System.out.println("商2"+x[1]/x[2]);
}catch(NullPointerException e){
System.out.println("一");
}catch(ArithmeticException e){
System.out.println("二");
}finally{
System.out.println("四");
}System.out.println("五");
}
}
结果:商1:2
四
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at c.Exceptions1.main(Exceptions1.java:66)
*/
/*
public class Exceptions1{
public static void main(String args[]){
int x[]={4,2,0};
try{
System.out.println("商1:"+x[0]/x[1]);
x[3]=100;
System.out.println("商2:"+x[1]/x[2]);
}catch(ArithmeticException e){
System.out.println("一");
}catch(RuntimeException e){
System.out.println("二");
throw e;
}catch(Exception e){
System.out.println("三");
}
finally{System.out.println("四");
} System.out.print("五");
}
}
商1:2
二
四
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at c.Exceptions1.main(Exceptions1.java:92)
第二个catch语句已经将异常捕获,但由throw又抛出一个异常,所欲新的异常无法捕获,程序中断
*/
public class Exceptions1{
public static void main(String args[]){
int x[]={4,2,0};
try{
System.out.println("商1:"+x[0]/x[1]);
x[3]=100;
System.out.println("商2:"+x[1]/x[2]);
}catch(ArithmeticException e){
System.out.println("一");
}catch(RuntimeException e){
System.out.println("二");
//return;
System.exit(1);//停止虚拟机运行
}catch(Exception e){
System.out.println("三");
}
finally{System.out.println("四");
} System.out.print("五");
}
}
/*商1:2
二
四
*/
/*
* 商1:2
二
*/
/*异常处理第二部分
* 1 语句格式
* try{
* 语句组1
* }
* catch(异常类1 对象名)
* {
* 语句组2
* }
* catch(异常类2 对象名)
* {
* 语句组3
* }
* ...
* finally{
* 语句组4
}
注:catch语句块的异常对象封装了异常事件发生的信息,在catch语句块中可以使用异常对象的方法
来获得这些信息;
getMessage():返回异常信息
printStackTrace():用来跟踪异常事件发生时执行堆栈的内容
finally语句块为异常处理提供了一个统一的出口,使得在控制流程转到其他部分以前,能够对
程序的状态做统一的管理。通常在finally语句块中进行资源清除工作,如关闭打开的文件,清除临时
文件。无论try文件是否抛出异常,finally语句块总要执行,finally语句不被执行的唯一情况是程序
先执行了System.exit()。如果catch代码块和finally代码块并存,finally代码块必须放在catch代码块后面。
*/
/*
public class Exceptions1 {
public static void main(String args[]){
int x[]={4,2,0};
try{
System.out.println("商1:"+x[0]/x[1]);
x[3]=100;
System.out.println("商2:"+x[1]/x[2]);
}catch(ArithmeticException e)
{System.out.print("一");
}
catch(RuntimeException e){
System.out.println("二");
}
catch(Exception e){
System.out.println("三");
}
finally{
System.out.println("四");
}
System.out.println("五");
}
}
商1:2
二
四
五
*/
//未捕获异常
/*
public class Exceptions1{
public static void main(String args[]){
int x[]={4,2,0};
try{
System.out.println("商1:"+x[0]/x[1]);
x[3]=100;
System.out.println("商2"+x[1]/x[2]);
}catch(NullPointerException e){
System.out.println("一");
}catch(ArithmeticException e){
System.out.println("二");
}finally{
System.out.println("四");
}System.out.println("五");
}
}
结果:商1:2
四
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at c.Exceptions1.main(Exceptions1.java:66)
*/
/*
public class Exceptions1{
public static void main(String args[]){
int x[]={4,2,0};
try{
System.out.println("商1:"+x[0]/x[1]);
x[3]=100;
System.out.println("商2:"+x[1]/x[2]);
}catch(ArithmeticException e){
System.out.println("一");
}catch(RuntimeException e){
System.out.println("二");
throw e;
}catch(Exception e){
System.out.println("三");
}
finally{System.out.println("四");
} System.out.print("五");
}
}
商1:2
二
四
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at c.Exceptions1.main(Exceptions1.java:92)
第二个catch语句已经将异常捕获,但由throw又抛出一个异常,所欲新的异常无法捕获,程序中断
*/
public class Exceptions1{
public static void main(String args[]){
int x[]={4,2,0};
try{
System.out.println("商1:"+x[0]/x[1]);
x[3]=100;
System.out.println("商2:"+x[1]/x[2]);
}catch(ArithmeticException e){
System.out.println("一");
}catch(RuntimeException e){
System.out.println("二");
//return;
System.exit(1);//停止虚拟机运行
}catch(Exception e){
System.out.println("三");
}
finally{System.out.println("四");
} System.out.print("五");
}
}
/*商1:2
二
四
*/
/*
* 商1:2
二
*/