I. What is an exception
Abnormal English words are exception, literal translation is the meaning of "accident, exception", that is, abnormal situation. In fact, exceptions are procedural errors in nature, including procedural logic errors and system errors. For example, the use of empty references, array subscript out of bounds, memory overflow errors, and so on, these are unexpected circumstances, deviating from the intentions of our program itself.
Exceptions are handled as objects in Java, the root class is the Java.lang.Throwable class, and many exception classes are defined in Java (such as OutOfMemoryError, NullPointerException, Indexoutofboundsexception, etc.), these exception classes fall into two main categories: Error and exception.
Error is an unhandled exception, such as OutOfMemoryError, which typically occurs, and the JVM chooses to terminate the program. So we don't need to be concerned with this kind of exception when we write programs.
Exception, which is something we often see, such as NullPointerException, indexoutofboundsexception, are exceptions that we can handle.
Exceptions to the exception class include checked exception and unchecked exception (unchecked exception also known as Run-time exception runtimeexception, Of course, the run-time exception here is not the exception that I described earlier in the run, but the term "runtime exception" is used in Java to indicate that the exception of the exception class occurs during runtime.
Unchecked Exception (non-check exception), also known as Run-time exceptions (runtimeexception), such as Common NullPointerException, indexoutofboundsexception. For run-time exceptions, the Java compiler does not require exception capture processing or claims to be thrown, which is the programmer's discretion.
Checked exception (check for exceptions), also known as non-runtime exceptions (exceptions other than runtime exceptions are non-runtime exceptions), the Java compiler forces programmers to have capture processing, such as common Ioexeption and SqlException. For non-runtime exceptions, if you do not capture or throw claim processing, the compilation will not pass
In Java, the parent class of all exception classes is the Throwable class, the error class is the parent of the error type exception, the exception class is the parent class of the exception type exception, and the RuntimeException class is the parent of all runtime exceptions. Classes other than RuntimeException and inheriting exception are non-runtime exceptions.
Typical runtimeexception include NullPointerException, indexoutofboundsexception, IllegalArgumentException and so on.
Typical non-runtimeexception include IOException, SqlException and so on.
Two. How exceptions are handled in Java
In Java, if you need to handle an exception, you must first capture the exception and then process the exception. How can exceptions be caught and handled by the code that might have occurred? Use the try and catch keywords to
The code that is surrounded by a try block indicates that this code may be an exception, and once an exception occurs, the exception is caught by a catch, and then the exception is handled in the catch block.
This is a way of handling exceptions. In Java also provides another kind of exception handling is thrown exception, as the name implies, that is, if an exception occurs, I throw this exception, let the caller go to deal with, I do not do the specific processing, at this time need to use the throw and throws keyword
In the case of exception handling in Java, there are three ways to do exception handling for code that might occur:
1) to code block with try: Catch for exception capture processing;
2) in the code of the method body external throws to throw the declaration, tell the caller of this method this code may appear these exceptions, you need to handle carefully. There are two scenarios:
If a declaration throws an exception that is a non-runtime exception, the caller of this method must be displayed with try: Catch block or continue to throw an exception to the upper layer.
If a declaration throws an exception that is a run-time exception, the caller of this method can selectively perform exception-trapping processing.
3) throws an exception object in the code block with a throw, and there are two cases, similar to 2, in this case:
If the exception object that is thrown is a non-runtime exception, the caller of this method must be displayed with try: Catch block or continue to throw an exception to the upper layer.
If the exception object that is thrown is a run-time exception, the caller of this method can selectively perform exception capture processing.
Java Basics-Exceptions