java中的异常处理

异常:就是在程序的运行过程中所发生的不正常的事件,它会中断正在运行的程序


当程序中有异常时就需要进行异常处理

异常处理:java变成语言使用异常处理机制为程序提供了错误处理的能力


异常处理的关键字:

try、catch、finally、throw、throws

异常实例1:除数与被除数(除数不能为0)


异常处理

public static void main(String[] args) {
		//包裹可能会出现的bug(异常)的代码
		try{
			Scanner sc = new Scanner(System.in);
			System.out.print("请输入被除数:");
			int a = sc.nextInt();
			System.out.print("请输入除数:");
			int b = sc.nextInt();
			System.out.println("运算结果:"+a/b);
		}catch(InputMismatchException ie){
			//输入内容不匹配的情况,就会进入这个异常处理
			System.out.println("输入的是非整数");
		}catch(ArithmeticException ae){
			//除数是零的时候,算数出现错误异常时,就会进入这个异常处理
			System.out.println("除数不能为0");
		}catch(Exception e){
			//除了上边出现的错误以外,其他任何异常发生,就会进入到这个异常处理
			System.out.println("程序出现未知错误");
		}finally{
			//b不管程序有没有异常,最终都要执行finally代码
			//finally是最终执行的代码块
			System.out.println("谢谢使用!");
		}
	}
异常处理实例2:

try{}里有一个return语句,那么紧跟在这个try后的finally{}里的代码会不会被执行,是在return前还是后?( A )
A,会执行,在return之后执行 B,不会执行,return直接返回
C,会执行,在return之前执行 D,需要根据具体判断
写一个实例来看:

public class Ch02 {
	public static int test(){
		int a;
		try{
			a = 10;
			return a;
		}finally{
			System.out.println("aaaaaaaaaaa");
			a = 20;
		}
	}	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.out.println(test());
	}
}
运行结果:

从运行结果可以看出:

try{}里有一个return语句,

那么紧跟在这个try后有finally{},

会执行finally代码,

但会在return之后


在异常处理的关键词中有throw和throws两个自定义异常


通过两个实例来解释两个关键字

1.关键字throws

public class Ch03 {
	/**
	 * throws
	 * 明显提示异常信息
	 * 声明方法可能会出现的异常
	 * 调用者必须要解决这些异常
	 * @throws ArithmeticException
	 * @throws InputMismatchException
	 * @throws Exception
	 */
	public static void test() throws ArithmeticException,InputMismatchException,Exception{
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入被除数:");
		int a = sc.nextInt();
		System.out.print("请输入除数:");
		int b = sc.nextInt();
		System.out.println("运算结果:"+a/b);
	}	
	public static void main(String[] args) {
		try {
			test();
		} catch (InputMismatchException e) {
			// TODO Auto-generated catch block
			//打印错误提示信息
			e.printStackTrace();
		} catch (ArithmeticException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
2.关键字throw

public class Ch04 {
	/**
	 * throw
	 * 不明显提示异常信息
	 * 作用:手动抛出异常
	 * 程序本身可能没有异常,但是不符合业务逻辑时
	 * 可以手动抛出异常
	 * @throws ArithmeticException
	 * @throws InputMismatchException
	 * @throws Exception
	 */
	public static void test(){
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入被除数:");
		int a = sc.nextInt();
		System.out.print("请输入除数:");
		int b = sc.nextInt();
		if(b == 1){
			throw new CanNotBeOneException();
		}
		if(b == 0){
			throw new ArithmeticException("除数不能为0");
		}
		System.out.println("运算结果:"+a/b);
	}
	public static void main(String[] args) {	
			test();	
	}
}
/**
 * 自定义异常
 * @author Administrator
 *
 */
class CanNotBeOneException extends RuntimeException{
	public CanNotBeOneException() {
		super("除数不能为1");
		// TODO Auto-generated constructor stub
	}	
}

java中异常的分类:

分为check异常(必须解决)和运行异常(可以不解决)

下面写一个实例解释一下

public class Ch05 {
	public void test(int a,int b) throws AException{
		//计算两数之商
		if(a == 0){
			//runtime异常可以不解决
			throw new BException();
		}
		if(b == 0){
			//check异常,必须解决
			throw new AException();
		}
	}
}
/**
 * checked异常,必须要解决
 * @author Administrator
 *
 */
class AException extends Exception{
	public AException() {
		super();
		// TODO Auto-generated constructor stub
	}
	public AException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}
}
/**
 * 运行时异常,可以不解决
 * @author Administrator
 *
 */
class BException extends RuntimeException{
	public BException() {
		super();
		// TODO Auto-generated constructor stub
	}
	public BException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}
}
最后说一下异常处理的注意事项:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值