Home >Java >JavaBase >what is java exception

what is java exception

angryTom
angryTomOriginal
2019-11-13 10:23:203344browse

what is java exception

What is java exception

Java exception is a method provided by Java to identify and respond to errors. Consistency mechanism.

The Java exception mechanism can separate the exception handling code and normal business code in the program, ensuring that the program code is more elegant and improving the robustness of the program. When exceptions are used effectively, exceptions can clearly answer the three questions of what, where, and why: the exception type answers "what" was thrown, the exception stack trace answers "where" it was thrown, and the exception information answers "why" is thrown.

java exception keyword

• try -- used for monitoring. 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.

• catch -- used to catch exceptions. catch is used to catch exceptions that occur in the try statement block.

• finally -- The finally statement block will always be executed. It is mainly used to recycle physical resources (such as database connections, network connections, and disk files) opened in try blocks. Only the finally block, after execution is completed, will come back to execute the return or throw statement in the try or catch block. If a statement such as return or throw is used in the finally block, it will not jump back to execution and stop directly.

• throw -- used to throw exceptions.

• throws -- used in method signatures to declare exceptions that may be thrown by the method.

Example:

public class Demo1 {
    public static void main(String[] args) {
        try {
            int i = 10/0;
            System.out.println("i="+i); 
        } catch (ArithmeticException e) {
              System.out.println("Caught Exception"); 
            System.out.println("e.getMessage(): " + e.getMessage()); 
            System.out.println("e.toString(): " + e.toString()); 
            System.out.println("e.printStackTrace():");
            e.printStackTrace(); 
        }
    }
}

Run result:

Caught Exception
e.getMessage(): / by zero
e.toString(): java.lang.ArithmeticException: / by zero
e.printStackTrace():
java.lang.ArithmeticException: / by zero
    at Demo1.main(Demo1.java:6)

php Chinese website, a large number of free Java introductory tutorials , welcome to online learning!

The above is the detailed content of what is java exception. 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:How to parse XML in javaNext article:How to parse XML in java