四 Java 异常处理

本文深入解析Java中的异常处理机制,包括try-catch-finally语句的用法,自定义异常类的实现,以及异常的分类(Error、Exception、RuntimeException)。详细解释ArrayIndexOutOfBoundsException和ArithmeticException,并通过示例代码演示如何捕获和处理这些异常。同时介绍了finally语句的作用以及如何使用try-catch-finally结构统一管理程序状态。
一、五个关键词:try catch finally throw throws
public class TestEx {
	public static void main(String[] args){
		int[] arr = {1,2,3};
	      System.out.println(arr[4]);  //,改为2输出正确,为3
              System.out.println(2/0);
 }
}  

//说明:ArrayIndexOutOfBoundsException 表示  数组下标越界异常(就是第4个,这个元素不在数组集合中)
//算术异常ArithmeticExecption  , 这里表示结果不能为0,一个整数“除以零”时,抛出此类的一个实例。


catch 用法
ae: 自己定义的异常类对象名,系统将异常对象传递给ae 里面,ae 相当于形参

将所有的错误打印出来

加一条ae.printStackTrace();

二、异常的分类

  • Error :称为错误,是系统的错误你管不了,你处理不了,由Java 虚拟机生成并抛出(开着车,前面山塌了,不能前进,你处理不了)
  •  Exception :所有异常类的父类,其子类对应了各种各样可能出现的异常事件,我们可以处理并且捕获(开着车,你的刹车出问题,停下来修)
    
  •  Runtime Exception :一类特殊的异常,如被0 除。(1.你写一个A除以B,但是可能出现一个被0除这个错误,如果B等于0就会报,你可以catch,也可以不catch。   2.开着车去越野,路上有些小石子, 你可以直接压过去,也可以扫一下这些小障碍)


import java.io.*;
public class TestEx {
	public static void main(String[] args){

		TestEx te = new TestEx();
		te.m(0);		//调用这个方法的地方  at EX.TestEx.main(TestEx.java:15)
	}
	void m(int i) throws ArithmeticException{
		if(i==0)
			throw new ArithmeticException("被除数为0");		//真正抛出异常的地方  at EX.TestEx.m(TestEx.java:19)
	}
	
	void f() throws FileNotFoundException, IOException{
		FileInputStream in = new FileInputStream("myfile.txt");
		int b;
		b = in.read();
		while(b != -1){
			
		}
	
	}
}


另外一种情况:



三、异常的捕获和处理

Try{
//可能抛出异常的语句
}catch(SomeException1 e )
{
……..
}catch(SomeException2 e )
{
…..
}finally{
……..
}

Try语句
1. Try{….}语句指定了一段代码,该代码是一次捕获并处理例外的范围。
2. 在执行过程中,该段代码可能会产生并抛出一种或多种异常类型的对象,它后面的catch 语句要分别对这些异
常做相应的处理。
3. 如果没有列外产生,所有的catch 代码都被略过不执行。

Catch语句
1. 在catch 语句块中是对异常进行处理的代码,每个try 语句块可以伴随一个或多个catch 语句,用于处理可能
产生的不同类型的异常对象。
2. 在catch 中声明的异常对象(catch(SomeException e))封装了异常事件发生的信息,在catch 语句块中可以
使用这个对象的一些方法获取这些信息。
3. 例如:
1) getMessage()方法,用来的到有关异常事件的信息。
2) printStackTrace()方法,用来跟踪异常事件发生时执行堆栈的内容。

Finally 语句
1. Finally 语句为异常处理提供一个统一的出口,使得在控制流程转到程序的其他部分以前,能够对程序的状态作
统一的管理。
2. 无论try 所指定的程序块中是否抛出列外,finally 所指定的代码都要被执行。
3. 通常在finally 语句中可以进行资源的清除工作,如:关闭打开的文件,删除临时文件。


  说明:假设你是一个客服部门普通员工,一搬的投诉抱怨都可以处理了。 但是有个拿着菜刀闹事的过来了,你处理不了,交给你上上级部门,如果你上级部门处理不了就交给他的上级部门,还处理不了,交给警察局得了 。

实在搞不定,在 main方法那 throw Exception 把所有的异常都抛出


public class Test {
	public static void main(String args[]){
		Test test = new Test();
		try{
			test.method1();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void method1()
			    throws Exception{method2();} 
	public void method2()
				throws Exception{method3();} 
	public void method3()throws Exception{
		throw new Exception
				("异常在 method3");
	}
}

import java.io.*;
public class TestEx {
	public static void main(String[] args){
	
	FileInputStream in = null;
	
	try{
		in = new FileInputStream("myfile.txt"); //FileInputStream 从文件里读数据的
		int b;
		b = in.read();
		while(b != -1){
		System.out.print((char) b);
		b = in.read();
	 }			//一个try可以跟着多个catch,我一把茶壶可以跟着多个茶碗,就是一个男的可以三妻四妾,嘿嘿,开玩笑~~~作者林语堂
	}catch(FileNotFoundException e){	//打开文件失败时,抛出此异常。
		e.printStackTrace();
	}catch(IOException e){  
		System.out.println(e.getMessage());
	}finally{
	    try{
			in.close();   //在finally 语句中可以进行资源的清除工作 关闭打开的文件
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
  }
	void m(int i) throws ArithmeticException{
		if(i==0)
			throw new ArithmeticException("被除数为0");		//真正抛出异常的地方  at EX.TestEx.m(TestEx.java:19)
	}
	
	void f() throws FileNotFoundException, IOException{
		FileInputStream in = new FileInputStream("myfile.txt");
		int b;
		b = in.read();
		while(b != -1){
			System.out.print((char)b);
			b = in.read();
		}	
	}
	
	void f2() throws Exception{
		try{
		f();
		}catch(FileNotFoundException e){
			System.out.println(e.getMessage());    //不能留空,不能把这个错误给我吞掉, 相关的错误信息打印出来。
		}catch(IOException e){
		 e.printStackTrace();
		}
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值