Java try catch finally statement

In the actual development, according to try catch statement execution, the try block and a catch block statements may not be completely executed, while others require handling code to be executed. For example, the program turns on some physical resources (e.g., database connections, network connections, and disk files and the like) in the try block, the physical resources to be explicitly recovered.

	Java的垃圾回收机制不会回收任何物理资源,垃圾回收机制只回收堆内存中对象所占用的内存。

In order to ensure a certain physical resource can be recovered try block open, exception handling mechanism provided finally block, and the Java provides automatic resource management (Automatic Resource Management) technique after 7.

try catch finally statement may be a statement block matching described previously using the following syntax:

try {
    // 可能会发生异常的语句
} catch(ExceptionType e) {
    // 处理异常语句
} finally {
    // 清理代码块
}

For the above format, whether or not abnormality occurs (except in special cases), the finally statement block code will be executed. Further, the finally statement and try statement can be used to match, syntax is as follows:

try {
    // 逻辑代码块
} finally {
    // 清理代码块
}

Note the following when using the try-catch-finally statement:

	异常处理语法结构中只有 try 块是必需的,也就是说,如果没有 try 块,则不能有后面的 catch 块和 finally 块;
	
	catch 块和 finally 块都是可选的,但 catch 块和 finally 块至少出现其中之一,也可以同时出现;
	
	可以有多个 catch 块,捕获父类异常的 catch 块必须位于捕获子类异常的后面;
	
	不能只有 try 块,既没有 catch 块,也没有 finally 块;
	
	多个 catch 块必须位于 try 块之后,finally 块必须位于所有的 catch 块之后。
	
	finally 与 try 语句块匹配的语法格式,此种情况会导致异常丢失,所以不常见。

In general, regardless of whether an exception is thrown, the finally block will be executed statements, the implementation process as shown below.

Here Insert Picture Description
try catch finally statement block implementation can be subdivided into the following three cases:

1. If the try block not throw an exception, the execution finally block directly after completing the try block, the catch and then finally, after the try statement block statement.

2. If the try block throws an exception, capturing and catch clause, terminating execution of the try block where the exception is thrown, the process goes to catch block match, then finally block executes. If the finally block not throw an exception, then proceed to try the catch finally statement block statement; if finally block throws an exception, the exception is passed to put the caller of the method.

3. If the try block thrown exception is not caught by any catch clauses, then finally block perform direct statements, and the abnormality is transmitted to the caller of the method.

Unless you call a method System.exit exit the virtual machine (int status) in the try block, catch block, otherwise no matter how the execution of the code in the try block or a catch block, what happens, finally block will always execute exception handling .

Usually not used like a finally block results in a return or throw statement of the method terminates, otherwise it will lead to try and return the catch block and throw statement failure.

When the Windows system starts, even without any operation, will be displayed at shutdown "Thank use." The following write Java programs using a try catch finally statement to this process, as follows:

import java.util.Scanner;

public class Test04 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Windows 系统已启动!");
        String[] pros = { "记事本", "计算器", "浏览器" };
        try {
            // 循环输出pros数组中的元素
            for (int i = 0; i < pros.length; i++) {
                System.out.println(i + 1 + ":" + pros[i]);
            }
            System.out.println("是否运行程序:");
            String answer = input.next();
            if (answer.equals("y")) {
                System.out.println("请输入程序编号:");
                int no = input.nextInt();
                System.out.println("正在运行程序[" + pros[no - 1] + "]");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("谢谢使用!");
        }
    }
}

Use the simulation code uses the system try catch finally statement in main () method. When the system starts to display the prompt, regardless of whether the program is running, or there was an accident when you run the program, the program will execute the statements in the finally block, which displays "Thank use!." The output results are shown below.

Windows 系统已启动!
1:记事本
2:计算器
3:浏览器
是否运行程序:
y
请输入程序编号:
2
正在运行程序[计算器]
谢谢使用!
Windows 系统已启动!
1:记事本
2:计算器
3:浏览器
是否运行程序:
y
请输入程序编号:
5
谢谢使用!
java.lang.ArrayIndexOutOfBoundsException: 4
    at text.text.main(text.java:23)
Windows 系统已启动!
1:记事本
2:计算器
3:浏览器
是否运行程序:
asdfasd
谢谢使用!
Published 457 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104736837