异常处理

本文深入解析Java异常处理机制,涵盖异常概念、分类、捕获与处理流程,以及自定义异常和异常链的创建方法,是Java开发者理解异常处理不可或缺的指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常的概念

1 Java异常是Java提供的用于处理程序中错误的一种机制。

2 所谓错误是指在程序运行的过程中发生的一些异常事件(如:数组下标越界)

3 设计良好的程序应该在异常发生时提供处理这些错误的方法,使得程序不会因为异常的发生而阻断或产生不可预见的结果。

4 Java程序的执行过程中如果出现异常事件,可以生成一个异常类对象,该异常对象封装了异常事件的信息并将并将被提交给Java运行时系统,这个过程称为抛出异常(throw)

5 当Java运行时系统接受到异常对象时,会寻找能处理这一异常的代码并把当前异常对象交给其处理,这一过程称为捕获(catch)异常。

import java.io.*;
public class TestEx {
	public static void main(String[] args) {
		/*
		int[] arr = {1, 2, 3};
		try {
			System.out.println(2/0); //try里面写可能遇到的错误
		}catch (ArithmeticException ae) {  //ae是我们自己定义的扑捉到的异常的名字,相当于一个形参,系统自己初始化
			System.out.println("异常");  //catch“扑捉”异常之后,处理异常
			ae.printStackTrace();
		}
		*/
		try {
			new TestEx().f2();
		}catch (IOException e) {
			e.printStackTrace();
		}
		/*
		try {
			new TestEx().m(0);
		}catch (ArithmeticException ae){
			ae.printStackTrace();
			System.out.println("出错了");
		}
		*/
		FileInputStream in = null;
		try {
			in = new FileInputStream("myfile.txt");
			int b;
			b = in.read();
			while(b != -1) {
				System.out.print((char) b);
				b = in.read();
			}
		}catch (IOException e) {
			System.out.println(e.getMessage());
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally {
			try {
				in.close();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
	void m(int i) throws ArithmeticException {
		if(i==0)
			throw new ArithmeticException("被除数为0");
	}
	
	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 IOException {
		f();
	}

}

异常的分类

Error:错误,程序不对其进行处理

Exception:所有异常类的父类,其子类对应了各种各样可能出现的异常事件。一般需要用户显示的声明或捕获

Runtime Exception:一类特殊的异常,用户可以不必对其进行处理

检查异常:需要手动添加try和catch语句

 

 异常的捕获和处理

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

> try代码段包含可能产生 例外的代码

> try代码段后跟有一个或多个catch代码段

> 每个catch代码段声明其能处理的一种特点类型的异常并提供处理的方法

> 当异常发生时,程序会中止当前的流程,根据获取异常的类型去执行相应的catch代码段。

> finally段的代码无论是否发生异常都有执行。

throw与throws

如果某个方法调用了会抛出异常的方法,那么必须调用try{...}catch{...}语句捕获异常(左代码块),或者添加throws声明,将异常抛出给更上一层的调用者(右)。

异常种类

使用自定义的异常

1 通过继承Java.lang.Exception类声明自己的异常类

2 在方法适当的位置 生成自定义异常的实例,并用throw语句抛出

3 在方法的声明部分用throws语句声明该方法可能抛出的异常

class MyException extends Exception {
	private int id;
	public MyException(String message,int id) {
		super(messagee);
		this.id = id;
	}
	public int getId() {
		return id;
	}
}
public class TestMyEx {
	public void regist(int num) throws MyException {
		if (num < 0) {
			throw new MyException("人数为负值,不合理",3);
		}
		System.out.println("登记人数 "+ num);
	}
	public void manager() {
		try {
			regist(100);
		}catch(MyException e) {
			System.out.println("等级失败,出错类型码 = "+ e.getId());
			e.printStackTrace();
		}
		System.out.print("操作失败");
	}
	public static void main(String[] args) {
		TestMyEx t = new TestMyEx();
		t.manager();
		
	}

}

异常链

package com.imooc.test;

public class ChainTest {
	
	/*
	 * test1():抛出“喝大了”异常
	 * test2():调用test1,捕获“喝大了”异常,并且包装成运行时异常,继续抛出 
	 * main方法中,调用test2,尝试捕获test2抛出的异常
	 */
	public static void main(String[] args) {
		ChainTest ct = new ChainTest();
		try {
			ct.test2();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void test1() throws DrunkException{
		throw new DrunkException("no drink");
	}
	
	public void test2() {
		try {
			test1();
		}catch(DrunkException e) {
			RuntimeException newExc = new RuntimeException("司机一滴酒,亲人两行泪");
			newExc.initCause(e);  //???
			throw newExc;  //???
		}
	}

}
package com.imooc.test;

public class ChainTest {
	
	/*
	 * test1():抛出“喝大了”异常
	 * test2():调用test1,捕获“喝大了”异常,并且包装成运行时异常,继续抛出 
	 * main方法中,调用test2,尝试捕获test2抛出的异常
	 */
	public static void main(String[] args) {
		ChainTest ct = new ChainTest();
		try {
			ct.test2();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void test1() throws DrunkException{
		throw new DrunkException("no drink");
	}
	
	public void test2() {
		try {
			test1();
		}catch(DrunkException e) {
			RuntimeException newExc = new RuntimeException(e);
	
			throw newExc;  //???
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值