知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

      • Theory:Errors in programs
      • Theory:What is an exception
      • Theory:NPE
      • Theory:Array exceptions
      • Theory:Hierarchy of exceptions
      • Theory:Exception Handling
        • How to handle an exception
        • The try-catch statement
        • Getting info about an exception
        • Catching multiple exceptions
        • The finally block
        • Conclusion
      • Theory:Throwing exceptions
      • Theory:Cutom exceptions
      • Theory:What is a bug
      • Theory:Introduction to logging
      • Theory:Debugging techniques
    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Error & Exception
Jim
2022-08-04
目录

Theory:Exception Handling

As you already know, an exception interrupts the normal execution of a program. Normally this is not what we want to happen. Luckily, it is possible to write some code that will handle the exception without stopping the whole program. To do that, Java provides an exception handling mechanism that works with both checked and unchecked exceptions.

# How to handle an exception

After a line of code throws an exception, the Java runtime system attempts to find a suitable handler for it. Such a handler can be located in the same method where the exception occurred or in the calling method. As soon as a suitable handler is found and executed, the exception is considered as handled and the program runs normally.

Technically, an exception can be handled in the method where it occurs or in the calling method. The best approach to handle an exception is to do it in a method that has sufficient information to make the correct decision based on this exception.

Let's now learn three keywords for handling exceptions: try, catch and finally.

# The try-catch statement

Here is a simple try-catch template for handling exceptions:

try {
    // code that may throw an exception
} catch (Exception e) {
    // code for handling the exception
}
1
2
3
4
5

The try block is used to wrap the code that may throw an exception. This block can include all lines of code, including method calls.

The catch block is a handler for the specified type of exception and all of its subclasses. This block is executed when an exception of the corresponding type occurs in the try block.

注意

Note that the specified type in a catch block must extend the Throwable class.

In the presented template, the catch block can handle exceptions of the Exception class and all classes derived from it.

The following example demonstrates the execution flow with try and catch.

System.out.println("before the try-catch block"); // it will be printed

try {
    System.out.println("inside the try block before an exception"); // it will be printed

    System.out.println(2 / 0); // it throws ArithmeticException

    System.out.println("inside the try block after the exception"); // it won't be printed
} catch (Exception e) {
    System.out.println("Division by zero!"); // it will be printed
}

System.out.println("after the try-catch block"); // it will be printed
1
2
3
4
5
6
7
8
9
10
11
12
13

The output:

before the try-catch block
inside the try block before an exception
Division by zero!
after the try-catch block
1
2
3
4

The program does not print "inside the try block after the exception" since the ArithmeticException aborted the normal flow of the execution. Instead, it executes the print statement in the catch block. After completion of the catch block, the program executes the next statement (printing "after the try-catch block") without returning to the try block again.

Replacing Exception with ArithmeticException or RuntimeException in the catch statement does not change the execution flow of the program. But replacing it with NumberFormatException will make the handler unsuitable for the exception and the program will fail.

提示

As we noted earlier, the try-catch statement can handle both checked and unchecked exceptions. But there is a difference: checked exceptions must be wrapped with a try-catch block or declared to be thrown in the method, while unchecked exceptions don't have to.

# Getting info about an exception

When an exception is caught by a catch block, it is possible to get some information on it:

try {
    double d = 2 / 0;
} catch (Exception e) {
    System.out.println(e.getMessage());
}
1
2
3
4
5

This code prints:

An exception occured: / by zero
1

# Catching multiple exceptions

It is always possible to use a single handler for all types of exceptions:

try {
    // code that may throw exceptions
} catch (Exception e) {
    System.out.println("Something goes wrong");
}
1
2
3
4
5

Obviously, this approach does not allow us to perform different actions depending on the type of exception that has occurred. Fortunately, Java supports the use of several handlers inside the same try block.

try {
    // code that throws exceptions
} catch (IOException e) {
    // handling the IOException and its subclasses    
} catch (Exception e) {
    // handling the Exception and its subclasses
}
1
2
3
4
5
6
7

When an exception occurs in the try block, the runtime system determines the first suitable catch block according to the type of the exception. Matching goes from top to bottom.

注意

Important, the catch block with the base class has to be written below all blocks with subclasses. In other words, the more specialized handlers (like IOException) must be written before the more general ones (like Exception). Otherwise, the code won't compile.

Since Java 7, you can use a multi-catch syntax to have several exceptions handled in the same way:

try {
    // code that may throw exceptions
} catch (SQLException | IOException e) {
    // handling SQLException, IOException and their subclasses
    System.out.println(e.getMessage());
} catch (Exception e) {
    // handling any other exceptions
    System.out.println("Something goes wrong");
}
1
2
3
4
5
6
7
8
9

In the code above SQLException and IOException (alternatives) are separated by the | character. They will be handled in the same way.

注意

Note that alternatives in a multi-catch statement cannot be each other's subclasses.

# The finally block

There is another possible block called finally. All statements present in this block will always execute regardless of whether an exception occurs in the try block or not.

try {
    // code that may throw an exception
} catch (Exception e) {
    // exception handler
} finally {
    // code always be executed
}
1
2
3
4
5
6
7

The following example illustrates the order of execution of the try-catch-finally statement.

try {
    System.out.println("inside the try block");
    Integer.parseInt("101abc"); // throws NumberFormatException
} catch (Exception e) {
    System.out.println("inside the catch block");
} finally {
    System.out.println("inside the finally block");
}

System.out.println("after the try-catch-finally block");
1
2
3
4
5
6
7
8
9
10

The output:

inside the try block
inside the catch block
inside the finally block
after the try-catch-finally block
1
2
3
4

If we remove the line that throws NumberFormatException, the finally block is still executed after the try block.

inside the try block
inside the finally block
after the try-catch-finally block
1
2
3

提示

Interesting: the finally block is executed even if an exception occurs in the catch block.

It is also possible to write try and finally without a catch block at all.

try {
    // code that may throw an exception
} finally {   
    // code always be executed
}
1
2
3
4
5

In this template, the finally block is executed right after the try block.

# Conclusion

The try-catch statement allows us to handle both checked and unchecked exceptions .

The try block wraps the code that may throw an exception while the catch block handles this exception in case it occurs, also allowing us to get some information about it. It is possible to use several handlers to provide different scenarios for different types of exceptions.

Finally, there's an optional finally block that is always executed. Its main feature is that it executes even if we fail to handle an exception at all.

编辑 (opens new window)
#Java#Exception
上次更新: 2022/09/26, 16:55:15
Theory:Hierarchy of exceptions
Theory:Throwing exceptions

← Theory:Hierarchy of exceptions Theory:Throwing exceptions→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式