Java technology _ Java 0021 _ What is an exception in java?
1. What is a java exception? java has encountered some problems during execution, resulting in execution interruption. This problem is called an exception.
Exceptions may occur for different causes, including:
= Invalid data entered by the user.
= The file to be opened does not exist.
= The network connection is lost.
= JVM has exhausted memory.
= Operate on null as an object. 2. What are exceptions? To learn how to handle exceptions in Java, you need to know three types of exceptions:
Checked exception)
Check exceptions are usually caused by user errors, which are not foreseeable to the programmer. For example, if a file is opened but cannot be found, an exception occurs. These exceptions cannot be found during compilation.
Runtime exception (runtime exception is also called unchecked exception)
Exceptions that can be avoided by the program during running. Instead of checking for exceptions, runtime exceptions are ignored during compilation. The running exception here is not the exception we mentioned during the running, but is expressed in Java in terms of running exception. In addition, all Exception exceptions are generated during running.
Error)
An exception that cannot be processed, such as OutOfMemoryError. Generally, the JVM terminates the program. Therefore, we do not need to care about such exceptions when writing programs.
3. What is the exception hierarchy?
In Java, the parent class of all Exception classes is the Throwable class, the Error class is the parent class of the error Type exception, and the Exception class is the parent class of the exception type Exception, the RuntimeException class is the parent class of all runtime exceptions. classes other than RuntimeException that inherit Exception are non-runtime exceptions.
Common RuntimeException include NullPointerException, IndexOutOfBoundsException, and IllegalArgumentException.
Common Non-RuntimeException types include IOException and SQLException.
4. What methods are provided for exceptions? The following are important methods in the Throwable class.
1Public String getMessage ()
Returns a detailed message about an exception. The message is initialized in the Throwable constructor.
2Public Throwable getCause ()
Returns the cause of an error indicated by a Throwable object.
3Public String toString ()
Returns the name of the getMessage () result.
4Public void printStackTrace ()
Print the toString () result and stack trace information to System. err, and output the error stream.
5Public StackTraceElement [] getStackTrace ()
Returns an array of stack trace information. Element 0 indicates the top of the stack, and the last element indicates the bottom of the stack.
For details about how to capture exceptions in java, see: how to capture exceptions in java
For how to throw an exception, see throws/throw.