从零开始学JAVA——异常处理

在局部内部类的方法中(比如:show)如果调用局部内部类所声明的方法(如:method)中的局部变量(比如:num)的话,
要求此局部变量声明为final的。

jdk7及之前版本:要求此局部变量显式声明为final
jdk8及以后的版本,可以省略final的声明

	public void method() {
		//局部变量
		final int num =10;
		class AA{
			
			public void show() {
//				num = 20;
				System.out.println(num);
			}
	}

/*

Error:

java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况错误

一般不编写针对性的代码进行处理

public class ErrorTest {
public static void main(String[] args) {
//1.栈溢出:java.lang.StackOverflowError
// main(args);
//堆溢出:java.lang.OutOfMemoryError
// Integer[] arr = new Integer[102410241024];

}

}
一、异常体系结构
在这里插入图片描述

public class ExceptionTest {
	//***********以下是编译时异常*****************
	@Test
	public void test7() {
		File file = new File("hello.txt");
		FileInputStream fis = new FileInputStream(file);
		
		int data = fis.read();
		while(data != -1) {
			System.out.print((char)data);
			data = fis.read();
		}
		fis.close();
	}
	
	//***********以下上运行时异常*****************
	//NullPointerException空指针异常
	@Test
	public void test1() {
//		int[] arr = new int[3];
//		System.out.println(arr[3]);
		
		String str = "abc";
		str=null;
		System.out.println(str.charAt(0));
	}
	
	@Test
	public void test2() {
		//ArrayIndexOutOfBundsException数组脚越界
//		int[] arr =new int[10];
//		System.out.println(arr[10]);
		//StringIndexOutOfBundsException
		String str = "abc";
		System.out.println(str.charAt(3));
	}
	//ClassCastException类型转换异常
	@Test
	public void test3() {
		Object obj = new Date();
		String str = (String)obj;
	}
	//NumberFormatException数字格式异常
	@Test
	public void test4() {
		String str = "123";
		str = "abc";
		int num = Integer.parseInt(str);
	}
	//InputMismatchException输入类型不匹配异常
	@Test
	public void test5(){
		Scanner scanner =new Scanner(System.in);
		 int score = scanner.nextInt();
		 System.out.println(score);
	}
	//ArithmeticException算术异常
	@Test
	public void test6() {
		int a =10;
		int b = 0;
		System.out.println(a/b);
	}
}

异常处理的方式二:throws+异常类型
1."throws + 异常类型"写在方法的声明处,指明此方法执行时,可能会抛出的异常类型。
一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对满足throws后异常类型时,就会被抛出。异常代码后续的代码,就不再执行!

2.体会:try-catch-finally:真正的将异常给处理掉了。
throws的方式只是将异常抛给了方法的调用者。并没有真正将异常处理掉。

3.开发中如何选择使用try-catch-finally还是使用throws?
3.1如果父类中被重写的方法没有throws方式处理异常,则子类重写的方法也不能使用throws,意味着如果子类重写的方法中有异常,必须使用try-catch-finally方式处理。
3.2执行方法A中,先后又调用了另外的几个方法,这几个发方法是递进关系执行的。我们建议这几个方法使用throws的方式进行处理。而执行方法A可以考虑使用try-catch-finally方式进行处理。

public class ExceptionTest2{
	public static void main(String[] args){
		try {
			method2();
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
//		method3();
	}
	
	public static void method3() {
		try {
			method2();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void method2() throws IOException{
		method1();
	}
	
	public static void method1() throws FileNotFoundException,IOException{
		File file = new File("hello.txt");
		FileInputStream fis = new FileInputStream(file);
		
		int data = fis.read();
		while(data != -1) {
			System.out.print((char)data);
			data = fis.read();
		}
		fis.close();
		System.out.println("haha");
	}
}

课后练习题一:

编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除

  • 对数据类型不一致(NumberFormatException)、缺少命令行参数(ArrayIndexOutOfBoundsException、
  • 除0(ArithmeticException)及输入负数(EcDef 自定义的异常)进行异常处理。
  • 提示
  • 1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能
  • 2)在main()方法中使用异常处理语句进行异常处理。
  • 3)在程序中,自定义对应输入负数的异常类(EcDef)。
  • 4)运行时接受参数java EcmDef 20 10 //args[0]=“20” args[1]=“10”
  • 5)Interger类的static方法parseInt(String s)将s转换成对应int值
  • 如:int a = Interger.parseInt(“314”) a=314

编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除

  • 对数据类型不一致(NumberFormatException)、缺少命令行参数(ArrayIndexOutOfBoundsException、
  • 除0(ArithmeticException)及输入负数(EcDef 自定义的异常)进行异常处理。
  • 提示
  • 1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能
  • 2)在main()方法中使用异常处理语句进行异常处理。
  • 3)在程序中,自定义对应输入负数的异常类(EcDef)。
  • 4)运行时接受参数java EcmDef 20 10 //args[0]=“20” args[1]=“10”
  • 5)Interger类的static方法parseInt(String s)将s转换成对应int值
  • 如:int a = Interger.parseInt(“314”) a=314
public class EcmDef {
	public static void main(String[] args) {
		try {
			int i = Integer.parseInt(args[0]);
			int j = Integer.parseInt(args[1]);
			int result = ecm(i, j);
			System.out.println(result);
		}catch(NumberFormatException e) {
			System.out.println("数据类型不一致");
		}catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("缺少命令行参数");
		}catch(ArithmeticException e){
			System.out.println("除0");
		}catch (EcDef e) {
			System.out.println(e.getMessage());
		}
	}
	
	public static int ecm(int i ,int j)  throws EcDef{
		if(i<0||j<0) {
			throw new EcDef("分子或分母为负数了!");
		}
		return i / j;

	}
}

自定义异常类

public class EcDef extends Exception{
	static final long serialVersionUID = -3387516993124229948L;
	
	public EcDef() {}
	public EcDef(String msg) {
		super(msg);
	}
}

面试题:常见的异常有哪些?举例说明

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值