Both Error
and Exception
are subclasses of Throwable
. In java, only instances of Throwable
type can be thrown or caught, which is the basic type of exception handling mechanism.
Exception
and Error
reflect the classification of different abnormal situations by the java platform designers. Exception
is an unexpected situation that can be expected in the normal operation of the program, which can be caught and dealt with accordingly.Error
refers to situations that are unlikely to occur under normal circumstances. Most Error
will cause the program to be in an abnormal and unrecoverable state, which does not need to be captured. The common OutOfMemoryError
is Error
A subclass of `.Exception
is divided into checkable exceptions (checked
) and unchecked exceptions (unchecked
). Checkable exceptions must be explicitly captured in the source code, which is part of the compile-time check and cannot be checked. Exceptions refer to runtime exceptions, such as NullPointerException
, ArrayIndexOutOfBoundsException
, etc., which are usually logical errors that can be avoided by coding. It is determined whether it needs to be caught according to needs, and it is not mandatory at compile time.throw
: Sometimes we explicitly want to create an exception object, and then throw it to stop the normal processing of the program. The throw
keyword is used to throw an exception to the runtime to handle it.throws
: When we throw any selected exception in a method without handling it, we need to use the throws keyword in the method signature to let the caller know about the exceptions that the method might throw. The caller method can handle these exceptions or propagate them to the caller method using the throws keyword. We can provide multiple exceptions in the throws
clause and it can also be used with the main()
method.try-catch
: We use a try-catch
block in our code to handle exceptions. try
is the start of the block, catch
handles exceptions at the end of the try
block. We can use try
to create multiple catch
blocks, or nest try catch
blocks. The catch
block requires an argument of type Exception
.finally:finally
block is optional and can only be used with a try-catch
block. Since exceptions stop the execution process, we may open some resources that will not be closed, so we can use finally
block. The finally
block always executes regardless of whether an exception occurs.ArrayIndexOutOfBoundsException
: Exception thrown when accessing an array with an invalid index *. If the index is negative or greater than or equal to the size of the array, the index is an illegal indexArithmeticException
: This exception is thrown when an abnormal operation condition occurs. (e.g. an instance of this class is thrown when an integer is "divided by zero")IllegaArguementException
: The exception thrown indicates that an invalid or incorrect parameter was passed to the methodNullPointerException
: Null pointer exception (calling an instance method of a null object, etc.)ClassCastException
: Class cast exceptionArrayStoreException
: Data storage exception, the type is inconsistent when operating the arrayClassNotFoundException
: Could not find a definition for the class with the specified nameDataFormatException
: Data format exceptionIOException
: Input and output exceptionSQLException
: exception that provides information about database access errors or other errorsFileNotFoundException
: This exception is thrown when trying to open the file represented by the specified path name failsEOFException
: This exception is thrown when the end of the file or stream is unexpectedly reached during input