Java专题 异常处理和finally处理的理解 +详解以及案例

这里使用算数的流程来做样例,注意事项使用注释来说明

	Scanner reader = new Scanner(System.in);
	System.out.println("Input the number: ");
	
	try {
		int num = reader.nextInt();				//try 中捕获到第一个异常后 后面的不会执行直接跳出try块  
	System.out.println(5/num);					//直接执行try块后面的正常语句
	
	int[] aa = {1,2,3};
	System.out.println(aa[3]);		//越界
	
	} catch (ArithmeticException e) {	//异常得到处理后程序会继续运行下去	
											//若使用Exception后面的子类异常没有写的必要了
		e.printStackTrace();  //打印异常信息
		System.out.println("catch the exception.");
	}catch (InputMismatchException e) {
		e.printStackTrace();
		System.out.println("bu pi pei.");
	}catch (IndexOutOfBoundsException e) {
		e.printStackTrace();
		System.out.println("out of band");
	}
	System.out.println("The end.");

如果可以确定异常的种类建议使用准确的异常类类来获取异常,如果不知道会出现什么异常就简单一点使用总的异常来获取

	System.out.println("Input the number.");
	try {
		
		System.out.println(5/(new Scanner(System.in).nextInt()));
		
	} catch (Exception e) {								//不知道异常类型的时候使用总的Exception
													//若知道具体类型建议详细写 针对异常进行独有的处理方法
		e.printStackTrace();
		System.out.println("Catch the exception.");
	}
	System.out.println("End");

Exception是所有异常的父类,都可以获取到

如果多个异常的处理方法一致可以使用逻辑判断符来处理异常

	System.out.println("Input the number.");
	try {
		
		System.out.println(5/(new Scanner(System.in).nextInt()));
		
	} catch (IndexOutOfBoundsException | InputMismatchException e) {//多个异常若有相同的处理方法 使用逻辑符号连接
		e.printStackTrace();
	}
	catch (Exception e) {
		e.printStackTrace();
		System.out.println("Catch the exception.");
	}
	System.out.println("End");

在一些场合中,如果想在一段代码发生异常后继续执行一些收尾操作,不想让异常影响到最后的操作可以使用finally 一般finally用在关闭数据库资源,或者文件读写时关闭读写,后面也会有介绍

	System.out.println("Input the number: ");
	try {
		System.out.println(31/new Scanner(System.in).nextInt());
		int[] aa2 = {12,34,56};
		System.out.println(aa2[3]);
		
	} catch (InputMismatchException e) {
		e.printStackTrace();
		System.out.println("Bu pi pei.");
	} catch (ArithmeticException e) {
		e.printStackTrace();
		System.out.println("suan fa cuo wu.");
		
	}finally {								//catch后  一定会执行的语句  若catch 中终止则不会继续
		System.out.println("finally.");
	}

finally中的语句一定会执行,不管发不发生异常,当然,如果你把虚拟机在这之前停掉的话自然不会执行了…

	System.out.println("Input :");
	try {
		System.out.println(3/new Scanner(System.in).nextInt());
		System.exit(0);
	} finally {														//没有异常处理
		System.out.println("我不管,我就是要执行,异常了也要执行。");
	}
	System.out.println("End.");								//没有处理异常 终止执行

还有一些可能会用到的异常所提供的方法如下所示

	System.out.println("Input :");
	try {
		System.out.println(3/new Scanner(System.in).nextInt());
		System.out.println();
	} catch (Exception e) {
		System.out.println("getcause  "+e.getCause());//打印原因
		System.out.println("getmessage  "+e.getMessage());//获取异常信息
		System.out.println("tostring  "+e.toString());//打印异常信息
		e.printStackTrace();//打印异常的调用栈轨信息
	}

也可以自定义异常,自己写一个异常类然后继承Exception

class IllegalSexException extends Exception{		//自定义异常类  Exception编译时异常 需要抛出或catch
															//若继承RuntimeException 运行时异常 则
	public IllegalSexException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public IllegalSexException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		// TODO Auto-generated constructor stub
	}

	public IllegalSexException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public IllegalSexException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	public IllegalSexException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值