欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > 尚硅谷2019版Java异常处理篇笔记记录

尚硅谷2019版Java异常处理篇笔记记录

2025/5/6 16:09:59 来源:https://blog.csdn.net/weixin_73617818/article/details/146922972  浏览:    关键词:尚硅谷2019版Java异常处理篇笔记记录

第7章 异常处理


1. 异常概述与异常体系结构

1.1 异常的定义

  • 异常:程序执行过程中发生的不正常情况。
  • 错误类型
    • Error:JVM无法解决的严重问题,如StackOverflowErrorOutOfMemoryError
    • Exception:可以通过代码处理的异常,如空指针、数组越界等。

1.2 异常分类

  • 运行时异常(RuntimeException):编译器不强制处理,通常是逻辑错误,如NullPointerExceptionArithmeticException
  • 编译时异常(Checked Exception):编译器强制处理,通常是外部因素导致的异常,如IOExceptionClassNotFoundException
异常类型对比表
特性运行时异常(RuntimeException)编译时异常(Checked Exception)
继承关系继承RuntimeException直接继承Exception
处理要求不强制处理必须显式处理
产生原因代码逻辑错误外部资源问题
典型示例NullPointerException, ClassCastExceptionIOException, SQLException
最佳处理方式修复代码逻辑try-catch或throws声明

1.3 异常体系结构

Throwable
├── Error
│   ├── StackOverflowError
│   └── OutOfMemoryError
└── Exception├── RuntimeException│   ├── NullPointerException│   ├── ArithmeticException│   └── ArrayIndexOutOfBoundsException└── IOException├── FileNotFoundException└── EOFException
树状图
Throwable
Error
Exception
StackOverflowError
OutOfMemoryError
RuntimeException
Checked Exception
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
IOException
ClassNotFoundException

2. 常见异常

2.1 常见运行时异常

异常类型描述示例代码
NullPointerException空指针异常String str = null; System.out.println(str.length());
ArrayIndexOutOfBoundsException数组越界异常int[] arr = new int[3]; System.out.println(arr[3]);
ArithmeticException算术异常(如除零)int a = 10 / 0;
ClassCastException类型转换异常Object obj = new Date(); String str = (String) obj;

2.2 常见编译时异常

异常类型描述示例代码
IOException输入输出异常FileInputStream fis = new FileInputStream("file.txt");
FileNotFoundException文件未找到异常FileInputStream fis = new FileInputStream("nonexistent.txt");
ClassNotFoundException类未找到异常Class.forName("com.example.NonExistentClass");

3. 异常处理机制

异常处理机制流程图

程序 JVM catch块 控制台 执行代码 正常执行 创建异常对象 匹配异常类型 处理异常 终止程序 打印堆栈跟踪 alt [有异常处理] [无处理] alt [正常流程] [异常发生] finally块始终执行 程序 JVM catch块 控制台

3.1 try-catch-finally

3.1.1 语法
try {// 可能抛出异常的代码
} catch (ExceptionType1 e) {// 处理ExceptionType1类型的异常
} catch (ExceptionType2 e) {// 处理ExceptionType2类型的异常
} finally {// 无论是否发生异常,都会执行的代码
}
3.1.2 执行流程
  1. try块:执行可能抛出异常的代码。
  2. catch块:捕获并处理异常。
  3. finally块:无论是否发生异常,都会执行。
3.1.3 示例
public class ExceptionTest {public static void main(String[] args) {try {int a = 10 / 0; // 抛出ArithmeticException} catch (ArithmeticException e) {System.out.println("捕获到异常:" + e.getMessage());} finally {System.out.println("finally块执行");}}
}

3.2 throws

3.2.1 语法
public void method() throws ExceptionType {// 可能抛出异常的代码
}
3.2.2 示例
public class ThrowsTest {public static void main(String[] args) {try {readFile();} catch (IOException e) {e.printStackTrace();}}public static void readFile() throws IOException {FileInputStream fis = new FileInputStream("file.txt");int b = fis.read();while (b != -1) {System.out.print((char) b);b = fis.read();}fis.close();}
}

4. 手动抛出异常

4.1 语法

throw new ExceptionType("异常信息");

4.2 示例

public class ThrowTest {public static void main(String[] args) {try {throw new IOException("手动抛出异常");} catch (IOException e) {System.out.println("捕获到异常:" + e.getMessage());}}
}

5. 用户自定义异常类

5.1 自定义异常类

  • 继承:通常继承RuntimeExceptionException
  • 构造器:提供多个构造器,方便传递异常信息。

5.2 示例

class MyException extends Exception {private int idnumber;public MyException(String message, int id) {super(message);this.idnumber = id;}public int getId() {return idnumber;}
}public class MyExpTest {public static void main(String[] args) {try {regist(-1); // 抛出MyException} catch (MyException e) {System.out.println("登记失败,出错种类:" + e.getId());}}public static void regist(int num) throws MyException {if (num < 0) {throw new MyException("人数为负值,不合理", 3);} else {System.out.println("登记人数:" + num);}}
}

5.3 异常处理关键字

关键字描述
try捕获异常
catch处理异常
finally无论是否发生异常,都会执行
throw手动抛出异常
throws声明方法可能抛出的异常

5.4 异常处理关键字对比表

关键字作用域使用场景示例代码片段
try代码块包裹可能抛出异常的代码try { ... }
catch紧随try块捕获并处理特定类型异常catch (IOException e) { ... }
finally最后执行块释放资源/清理操作finally { fis.close(); }
throw方法内部主动抛出异常对象throw new Exception("msg");
throws方法声明处声明可能抛出的异常类型void read() throws IOException

5.5 异常处理流程

try {// 可能抛出异常的代码
} catch (Exception e) {// 处理异常
} finally {// 无论是否发生异常,都会执行
}

6. 异常处理最佳实践

6.1 处理策略对比

场景try-catch处理throws声明
当前方法能处理异常✅ 优先使用❌ 不适用
需要资源释放✅ 必须配合finally使用❌ 无法保证资源释放
方法不具备处理能力❌ 不应捕获✅ 声明throws
框架底层代码❌ 避免过度捕获✅ 向上传递

6.2 自定义异常设计原则

是否需要运行时异常
继承RuntimeException
继承Exception
提供多个构造器
包含错误代码字段
添加业务相关方法

7. 综合应用示例

7.1 文件读取标准模板

public class FileProcessor {public void processFile(String path) {FileInputStream fis = null;try {fis = new FileInputStream(path);// 处理文件内容...} catch (FileNotFoundException e) {System.out.println("文件未找到: " + e.getMessage());} catch (IOException e) {System.out.println("IO异常: " + e.getLocalizedMessage());} finally {if (fis != null) {try {fis.close();} catch (IOException e) {System.out.println("关闭流失败: " + e.getMessage());}}}}
}

7.2 异常转译模式

public class Service {public void businessOperation() throws BusinessException {try {// 调用DAO层方法new Dao().dataAccess();} catch (SQLException e) {// 将底层异常转换为业务异常throw new BusinessException("业务操作失败", e);}}
}class BusinessException extends Exception {public BusinessException(String msg, Throwable cause) {super(msg, cause);}
}

8. 异常处理性能优化

8.1 异常使用禁忌表

错误用法问题描述改进方案
空的catch块异常被静默吞噬至少记录日志
捕获Throwable可能捕获Error导致程序无法终止明确捕获具体异常类型
频繁抛出异常控制流程性能低下(比普通返回慢100倍)使用状态码或返回值控制流程
忽略资源关闭导致资源泄漏使用try-with-resources语法

8.2 try-with-resources示例(Java7+)

public class ResourceExample {public static void main(String[] args) {try (FileInputStream fis = new FileInputStream("test.txt");BufferedReader br = new BufferedReader(new InputStreamReader(fis))) {String line;while ((line = br.readLine()) != null) {System.out.println(line);}} catch (IOException e) {System.out.println("自动关闭资源,异常信息: " + e.getMessage());}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词