Java学习之路(十)异常处理

一、异常和错误

错误:在我们编写程序的过程中会经常发生的,包括编译期间和运行期间的错误。

异常:在程序运行过程中,意外发生的情况,背离我们程序本身的意图的表现,都可以理解为异常。

二、异常分类

  • Throwable(异常顶层)
    • Error(错误):程序无法处理的错误,表示运行应用程序中较严重的问题。
    • Exception(异常):程序本身可以处理的异常。异常处理通常指针对这种类型异常的处理。
      • Unchecked Exception:非检查异常
      • Checked Exception:检查异常

e00caq.png

三、异常处理

通过五个关键字来实现:try、catch、finally、throw(手动抛出异常,通常配合try catch或throws使用)、throws

(一)try…catch…

try{
    // 执行后可能产生异常的代码
}
catch (/*异常捕捉*/){
    // 异常处理
}
finally{
    // 不论是否发生异常,都会执行的代码
}

案例如下:

public class TestMain {
	
	public void compute() {
		int x = 10, y = 0;
		double z = 0;
		
		try {
			z = x / y;
			System.out.println(z);
		} catch (ArithmeticException e) {
			System.out.println("除数不得为0");
		} catch (Exception e) {		// 通常会在最后加上这个,来处理可能被遗漏的异常
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		TestMain testMain = new TestMain();
		testMain.compute();

	}

}

(二)throws

public void method() throws 异常类型{
    // 执行后可能产生异常的代码
}

案例如下

public class TestMain {
		
	public void compute2() throws ArithmeticException{
		int x = 10, y = 0;
		double z = 0;
		
		z = x / y;
		System.out.println(z);
	}
	
	public static void main(String[] args) {
		TestMain testMain = new TestMain();
		
		try {
			testMain.compute2();
		} catch (Exception e) {
			System.out.println("除数不得为0");
		}

	}

}

(三)throw

  1. 配合try…catch…

    public class TestMain {
    
    	public void compute3() {
    		int x = 10, y = 0;
    		double z = 0;
    		
    		try {
    			if (y == 0) {
    				throw new Exception("除数不能为0");
    			}else {
    				z = x / y;
    				System.out.println(z);
    			}
    				
    		} catch (ArithmeticException e) {
    			System.out.println("除数不得为00");
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public static void main(String[] args) {
    		TestMain testMain = new TestMain();
    		testMain.compute3();
    
    	}
    
    }
    
    /* 抛出异常:
    java.lang.Exception: 除数不能为0
    */
    
  2. 配合throws

    public class TestMain {
    	
    	public void compute4() throws Exception{
    		int x = 10, y = 0;
    		double z = 0;
    		
    		if (y == 0) {
    			throw new Exception("除数不能为0");
    		}else {
    			z = x / y;
    			System.out.println(z);
    		}
    	}
    	
    	public static void main(String[] args) {
    		TestMain testMain = new TestMain();
            
    		try {
    			testMain.compute4();
    		} catch (Exception e) {
    			System.out.println("除数不得为0");
    		}
    	}
    
    }
    /* 运行结果:
    除数不得为0
    */
    

四、自定义异常

取用上面的案例

public class DivException extends Exception {
	
	public DivException() {
		super("除数不得为0");
	}
}
public class TestMain {
	// throw与try catch
	public void compute3() {
		int x = 10, y = 0;
		double z = 0;
		
		try {
			if (y == 0) {
				throw new DivException();	// 抛出自定义异常
			}else {
				z = x / y;
				System.out.println(z);
			}
				
		} catch (ArithmeticException e) {
			System.out.println("除数不得为00");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void compute4() throws Exception{
		int x = 10, y = 0;
		double z = 0;
		
		if (y == 0) {
			throw new DivException();	// 抛出自定义异常
		}else {
			z = x / y;
			System.out.println(z);
		}
	}
	
    // throw与throws搭配
	public static void main(String[] args) {
		TestMain testMain = new TestMain();
		testMain.compute3();
		
		try {
			testMain.compute4();
		} catch (Exception e) {
			System.out.println("除数不得为0");
		}
	}

}

五、异常链

异常链:将异常发生的原因一个传一个串起来,即将底层异常信息传给上层,这样逐层抛出。

public class TestMain {
	
	public void compute() throws DivException{
		int x = 10, y = 0;
		double z = 0;
		
		try {
			z = x / y;
			System.out.println(z);
		} catch (ArithmeticException e) {
			throw new DivException();
		}
	}
	
	public void testExcept1() throws Exception{
		try {
			compute();
		} catch (Exception e) {
			throw new Exception("新异常1", e);
		}
	}
	
	public void testExcept2() throws Exception{
		try {
			testExcept1();
		} catch (Exception e) {
			throw new Exception("新异常2", e);
		}
	}
	
	public static void main(String[] args) {
		TestMain testMain = new TestMain();
		try {
			testMain.testExcept2();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

运行结果:

e00RiV.png

如果将代码中的y = 0改成y = 2,则运行结果为5.0。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值