Java中异常处理机制

异常可能是在程序执行过程中产生的,也可能是程序中throw主动抛出的。异常产生后,会使用一个对应类型的异常对象包装该异常,尝试寻找异常处理程序来处理异常。异常处理机制可以在程序发生异常时,对其执行预先设定的异常处理逻辑。

Java标准库内建了一些通用的异常。Throwable派生Error、Exception。Error类及其子类代表的是JVM本身的错误,并非程序的问题,不能由程序员通过代码进行处理。Exception及其子类代表的是在程序运行过程中产生的不期望发生的异常事件,可以被Java异常处理机制进行处理,是异常处理的核心。

1.异常被成功catch之后,代码会继续向后执行

package com.cb;

public class TestOne {
	
	public static void main(String args[]){
		
		int a=5;
		int b=0;
		try {
			int c=a/b;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("end");
	}
	
}

ArithmeticException是Exception的子类,能被ArithmeticException捕获的异常,使用Exception一定也可以捕获到。

执行结果:

 

2.异常未被成功catch,则不会继续向下执行代码

package com.cb;

public class TestOne {
	
	public static void main(String args[]){
		
		int a=5;
		int b=0;
		try {
			int c=a/b;
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		
		System.out.println("end");
	}
	
}

在上述代码中,异常未被成功捕获,继续向上抛,后续代码不会继续执行。

执行结果:

3.成功捕获异常后默默吞掉,代码继续向后执行

package com.cb;

public class TestOne {
	
	public static void main(String args[]){
		
		int a=5;
		int b=0;
		try {
			int c=a/b;
		} catch(ArithmeticException e) {
			//e.printStackTrace();
			//throw e;
		}
		
		System.out.println("end");
	}
}

异常被捕获并被默默吞掉,此种方式不可取。正常情况下要输出错误相关堆栈信息,或输出可以帮助定位问题的日志信息。

执行结果:

 

4.成功捕获异常后使用throw直接抛出,不对异常进行处理,代码不会继续向下执行

package com.cb;

public class TestOne {
	
	public static void main(String args[]){
		
		int a=5;
		int b=0;
		try {
			int c=a/b;
		} catch(ArithmeticException e) {
			//e.printStackTrace();
			throw e;
		}
		
		System.out.println("end");
	}
}

捕获异常后直接throw抛出

执行结果:

 

5.方法名后使用throws声明可能会抛出的异常,异常产生后交给上层调用方法处理

package com.cb;

public class TestOne {
	
	public static void main(String args[]){
		
		try {
			TestOne testOne=new TestOne();
			testOne.myMethod();
		} catch (ArithmeticException e) {
			e.printStackTrace();
		}
		
		System.out.println("main end");
	}
	
	public void myMethod() throws ArithmeticException{
		int a=5;
		int b=0;
		int c=a/b;
		
		System.out.println("myMethod end");
	}
}

myMethod方法执行代码抛出异常后,myMethod后续代码停止向下执行,异常抛出交给main处理,main成功捕获并进行处理,继续执行main方法后续代码,输出"main end"

执行结果:

 

6.try-catch-finally成功捕获异常,执行finally中代码,并继续执行try-catch-finally块后面的代码

package com.cb;

public class TestOne {
	
	public static void main(String args[]){
		int a=5;
		int b=0;
		try {
			int c=a/b;
			System.out.println("d=10");
		} catch (ArithmeticException e) {
			e.printStackTrace();
		}finally{
			System.out.println("execute finally code block");
		}
		
		System.out.println("main end");
	}
	
}

执行结果:

 

7.try-catch-finally未成功捕获异常,执行finally中代码,停止执行try-catch-finally块后面的代码

package com.cb;

public class TestOne {
	
	public static void main(String args[]){
		int a=5;
		int b=0;
		try {
			int c=a/b;
			System.out.println("d=10");
		} catch (NullPointerException e) {
			e.printStackTrace();
		}finally{
			System.out.println("execute finally code block");
		}
		
		System.out.println("main end");
	}
	
}

执行结果:

 

欢迎关注个人微信公众号“我爱编程持之以恒”。

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值