Home > Java > javaTutorial > body text

What does try mean in java

(*-*)浩
Release: 2020-09-17 16:00:02
Original
18085 people have browsed it

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
}
Copy after login

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");
        }
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template