Exception Handling in Java

Source: Internet
Author: User
Tags getmessage throwable

Tag: Bar ignores instant POI object storage based on round output logs

To understand how Java exception handling works, you need to master the following three types of exceptions:

    • Check for exceptions (non-run-time exceptions): The exception that is generated by the interaction between the program and the Environment , the most representative of the inspection exception is the user error or the problem caused by the exception, which the programmer can not foresee. For example, to open a nonexistent file, an exception occurs, and these exceptions cannot be simply ignored at compile time. ( processing: Must be try-catch, or throws out )
    • Non-inspection exception (runtime exception): Non-inspection exceptions are exceptions that can be avoided by programmers due to the exception generated by the program's writing errors . As opposed to an inspection exception, non-inspection exceptions can be ignored at compile time. ( Processing: Checking the modification program, for example, can add judgment statements, etc. )
    • Error: The error is not an exception, the program cannot handle it, and the thread termination is generally selected. Errors are usually ignored in the code. For example, when a stack overflows, an error occurs, and they are not checked for compilation.

Java Exception class hierarchy diagram:

In the Java built-in class (which is explained next), there are a large number of common check and non-inspection exceptions.

Java built-in exception class

The Java language defines some exception classes in the Java.lang standard package.

The subclass of the standard run-time exception class is the most common exception class. Since the Java.lang package is loaded into all Java programs by default, most exceptions inherited from runtime exception classes can be used directly.

Java also defines a number of other exceptions based on the class library, and the following table lists the non-checked exception classes for Java.

Exception Description
ArithmeticException This exception is thrown when an operating condition of an exception occurs. For example, an integer "divided by 0" throws an instance of this class.
ArrayIndexOutOfBoundsException The exception that is thrown when an array is accessed with an illegal index. If the index is negative or greater than or equal to the array size, the index is an illegal index.
Arraystoreexception The exception that is thrown when attempting to store an object of the wrong type to an array of objects.
ClassCastException This exception is thrown when an attempt is made to cast an object to a subclass that is not an instance.
IllegalArgumentException The exception thrown indicates that an invalid or incorrect parameter was passed to the method.
Illegalmonitorstateexception The exception thrown indicates that a thread has tried to wait on the object's monitor, or has attempted to notify other monitors that are waiting on the object and that it does not already have a monitor specified.
IllegalStateException The signal generated when a method is called at an illegal or inappropriate time. In other words, the Java environment or Java application is not in the proper state required by the requested operation.
Illegalthreadstateexception The exception that is thrown when the thread is not in the appropriate state required by the request operation.
Indexoutofboundsexception Indicates that a sorted index (for example, sorting an array, string, or vector) is out of range when thrown.
Negativearraysizeexception If the application attempts to create an array with a negative size, the exception is thrown.
NullPointerException This exception is thrown when an application tries to use the object where it is needed null
NumberFormatException This exception is thrown when an application attempts to convert a string to a numeric type, but the string cannot be converted to the appropriate format.
SecurityException An exception that is thrown by the security manager, indicating that there is a security violation.
Stringindexoutofboundsexception This exception is String thrown by the method, indicating that the index is either negative or exceeds the size of the string.
Unsupportedoperationexception The exception is thrown when the requested operation is not supported.

The following table lists the check exception classes that Java defines in the Java.lang package.

Exception Description
ClassNotFoundException When an application attempts to load a class, the corresponding class is not found, and the exception is thrown.
Clonenotsupportedexception ObjectThis exception is thrown when a method in the calling class clone clones an object, but the object's class cannot implement the Cloneable interface.
Illegalaccessexception When a class is denied access, the exception is thrown.
Instantiationexception Class newInstance This exception is thrown when an attempt is made to create an instance of a class using a method in the class, and the specified class object cannot be instantiated because it is an interface or an abstract class.
Interruptedexception One thread was interrupted by another thread, throwing the exception.
Nosuchfieldexception The requested variable does not exist
Nosuchmethodexception The requested method does not exist
Exception method

The following list is the primary method of the Throwable class:

Serial Number method and Description
1 Public String GetMessage ()
Returns detailed information about the exception that occurred. This message is initialized in the constructor of the Throwable class.
2 Public Throwable Getcause ()
Returns a Throwable object that represents the reason for the exception.
3 Public String toString ()
Returns the string-level name of the class using the result of GetMessage ().
4 public void Printstacktrace ()
Prints the ToString () result and stack level to System.err, which is the error output stream.
5 Public Stacktraceelement [] Getstacktrace ()
Returns an array containing the stack hierarchy. The element labeled 0 represents the top of the stack, and the last element represents the bottom of the method call stack.
6 Public Throwable Fillinstacktrace ()
Populates the Throwable object stack hierarchy with the current call stack hierarchy, adding to any previous information in the stack hierarchy.
Catching exceptions

Use the try and catch keywords to catch exceptions. The Try/catch code block is placed where the exception can occur.

The code in the Try/catch code block is called the Protection Code, and the syntax for using Try/catch is as follows:

try{   //program code}catch (exceptionname E1) {   //catch block}

The Catch statement contains the declaration to catch the exception type. When an exception occurs in the protection code block, the catch block after the try is checked.

If the exception that occurs is contained in a catch block, the exception is passed to the catch block, which is the same as passing a parameter to the method.

Exception Handling in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.