Home >Java >javaTutorial >What does try mean in java

What does try mean in java

(*-*)浩
(*-*)浩Original
2019-05-22 13:50:1218110browse

try is a keyword in Java, mainly used for exception handling mechanism, so what does it do?

What does try mean in java

try – used for listening. Place the code to be monitored (code that may throw exceptions) within the try statement block. When an exception occurs within the try statement block, the exception is thrown.

It is generally used in combination with the catch..finally block to guide the first part of the keyword and is used to declare the exception that needs to catch the specified statement block.

The complete usage method is:

try {
    语句块1
} catch (<? extends Throwable> e) {
    语句块2.1
} catch (<? extends Throwable> e) {
    语句块2.2
} catch (<? extends Throwable> e) {
    语句块2.3
...
} finally {
    语句块3
}

catch can appear 0, 1 or more times, finally can appear 0 or 1 times, but catch and finally They cannot both appear at the same time.

Example:

public class Test {
    public static void main(String[] args) {
        int i = 10;
        try {
            System.out.println(i / 0);//会出现异常
        }  catch(ArithmeticException ame) {
            ame.printStackTrace();
        } finally {
            System.out.println("byebye");
        }
    }
}

Related learning recommendations: java basic tutorial

The above is the detailed content of What does try mean in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:javac.exe What is it?Next article:javac.exe What is it?