J2SE第四章——异常(一)

java运行可能出错的阶段:

         开发阶段:.java  比如:关键字拼写错误,括号不匹配

         编译阶段: .class         javac编译源文件的时候就能校验源文件中的基础错误

         运行阶段: 出错,违背了某种规则,如果出错,就需要解决调试错误。

 

                   1.开发人员希望JVM提供什么错误信息?

                            哪里出错?

                            怎么出错?

 

                   (许多类,许多方法,许多行)----希望被提供:类名,方法名,行号,描述

 

                   2. 在java中一切皆为对象,所以错误信息也应该为对象。-----类

 

 

                   3. 运行期出错:JVM 错误类  (类名,方法名,行号,描述)

                            --->>  解决运行期出错的一种机制----异常

1.异常

         某些操作存在出错的可能,要求在书写源代码的时候:

  1.1 使用关键字try 把可能在运行期出错的代码括起来

public class Test01 {
	public static void main(String[] args) {
		try {
		  int i = 10/0;
		}

		System.out.println(i);
	}
}

1.2 运行时,如果该部分代码不出错,当然好;如果出错了,怎么办?

                            如果出了错 catch 捕获                           

                            1.2.1处理  catch(ArithmeticException e){} 其中变量e指向封装了错误信息的异常对象

                            1.2.2不处理,抛出去 throw e(不能越级,谁调用抛给谁)

                                               又因为方法调用是通过方法名实现方法调用,也就是说,调用方看不到方法内部“ throw e ”

                                               所以,有必要在方法的定义位置声明方法抛出了对象 " 用 throws + 异常类的名称

public class Test01 {
	public static void main(String[] args) throws ArithmeticException {
		try {
			int i = 10/0;
		} catch (ArithmeticException e) {
			// e.printStackTrace();
						
			throw e;
		}
 
		System.out.println(i);
	}
}


public class Test {
	public static void main(String[] args) {
		m1();

		try {
			m2();
		} catch (ArithmeticException e) {
			e.printStackTrace();
			throw e;
		}
		
	}

	public static void m1() {
		try {
			int i = 10/0;
		} catch (ArithmeticException e) {
			e.printStackTrace();
		}
	}

	public static void m2() throws ArithmeticException{
		try {
			int i = 10/0;
		} catch (ArithmeticException e) {
			throw e;
		}
	}
}

main方法去调用m2(),因为m2()的声明中有 "throws ArithmeticException" ,所以在调用m2()时,要使用try{}catch{}


场景:

         java程序  打开文件

         public File(Stringpathname)

public class Test {
	public static void main(String[] args) {

	}

	//处理异常
	public static void m1() {
		try {
			FileInputStream fil = new FileInputStream("e:/a.jdp");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	//不处理异常 往外抛  
	public static void m2() throws FileNotFoundException {
		try {
			FileInputStream fil = new FileInputStream("e:/a.jdp");
		} catch (FileNotFoundException e) {
			throw e;
		}	
	}


	//不处理异常(往外抛) --- 推荐的代码书写格式,不想处理异常,直接throws
	public static void m3() throws FileNotFoundException {
	}
}

应用:

       有可能出现若干“种”错误 

        答:每一种错误分别分装成一个异常类对象。如果处理,使用catch分别捕捉

public static void m3() {
	try {
		int i = 10/0;	// 除数不能为0 ArithmeticException
		int[] arr = {1,2,3};
		System.out.println(arr[5]);			//数组索引越界 ArrayIndexOutofBoundsException
	} catch (ArithmeticException e) {
		e.printStackTrace();
	} catch (ArrayIndexOutofBoundsException e) {
		e.printStackTrace();
	}

}
public static void m4() throws ArithmeticException ,ArrayIndexOutofBoundsException {
	int i = 10/0;	// 除数不能为0 ArithmeticException
	int[] arr = {1,2,3};
	System.out.println(arr[5]);	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值