Home >Java >javaTutorial >A brief introduction to Java exceptions (picture)
What you see on paper is shallow, but you know you have to do it in detail
--Lu You Ask the canal how clear it is so that there is a source of running water --Zhu Xi
1. The super class of java exception is java.lang.Throwable
Exception handling Class structure composition:
2. Java exceptions are divided into two categories: Error and Exception:
Error It is an error that the program cannot handle, such as OutOfMemoryError, ThreadDeath, etc. When these exceptions occur, the JVM selects the thread to terminate.
Exception is an exception that can be handled by the program itself. There are two types:
(1) Non-runtime exception (occurring in the compilation phase, called checkException ): Generally refers to errors that do not follow the Java language specifications, which are easier to see.
Such as NullPointerException, IndexOutOfBoundsException, these exceptions are unchecked exceptions, and the program can choose to capture them or not handle them.
These exceptions are generally caused by program logic errors. The program should avoid the occurrence of such exceptions as much as possible from a logical perspective.
(2) Runtime exception (occurring during the running of the program, called uncheckException): An exception that generally occurs during the running of the program, such as Null pointer errors and so on.
3. Java exceptions involve 5 keywords: try, catch,finally, throw, throws try{
尝试运行的程序代码
}
catch(异常类型 异常变量名)
{
异常处理代码,同时catch可以是多个,但只运行其中的一个
}
finally()
{
finally总在方法返回前运行,finally语句部分只有一个
}
The throws keyword is used in the method declaration part outside the method body to declare what exceptions the method may throw.
4. Common Exception:
NullPointException 空指针异常 ClassNotFoundException 找不到类 ClassCastException 类型转换 ArrayIndexOutOfBoundsException 数组越界
5. Finally()
When there are
return statements in try and catch, finally will be executed.
The above is the detailed content of A brief introduction to Java exceptions (picture). For more information, please follow other related articles on the PHP Chinese website!