RuntimeException and Checkedexception

Source: Internet
Author: User

Java exceptions are divided into two main categories: checked exceptions and runtime exceptions (runtime Exceptions). All RuntimeException classes and their subclass instances are called runtime exceptions, and exception instances that are not runtimeexception classes and their subclasses are called checked exceptions.

Only the Java language provides checked exceptions, and no other languages provide checked exceptions. Java considers that checked exceptions are exceptions that can be handled (fixed), so Java programs must explicitly handle checked exceptions.

There are two ways to handle checked exceptions:

(1) The current method explicitly knows how to handle the exception, and the program should use the Try...catch block to catch the exception, and then fix the exception in the corresponding catch block.

(2) The current method does not know how to handle this exception, and the exception should be declared when the method is defined.

Runtime exception is more flexible, runtime exception does not need to explicitly declare thrown, if the program needs to catch runtime exception, you can also use Try...catch block to catch runtime exception.

When you throw an exception by using the throw statement, if the exception thrown by the throw statement is a checked exception, the throw statement is either in the try block, explicitly capturing the exception, or in a method with a throws declaration, which gives the caller of the method the exception to handle In other words, when a checked exception arises (whether it is thrown by itself or thrown by the system), it is necessary to deal with it, not ignore it, or explicitly capture it in the Try...catch block, handle it, or put it in a method with throws declaration, The exception is given to the caller of the method for processing.

If the exception thrown by the throw statement is a runtime exception, then the statement does not need to be placed in the try block or in a method with the throws declaration thrown, and the program can either explicitly use Try...catch to catch and handle the exception, or not handle the exception at all. The exception is given to the method caller for processing. (http://blog.163.com/[email protected]/blog/static/6884772520126394334182/)

ArithmeticException (Exception with divisor 0), bufferoverflowexception (overflow exception on buffer), bufferunderflowexception (buffer underflow exception), Indexoutofboundsexception (out-of-bounds exception), nullpointerexception (null pointer exception), Emptystackexception (empty stack exception), IllegalArgumentException (illegal parameter exception), Negativearraysizeexception, Nosuchelementexception, SecurityException, SystemException, Undeclaredthrowableexception

1. Java.lang.NullPointerException
The explanation of the exception is that "the program encounters a null pointer", which simply means that an uninitialized object or a nonexistent object is called, which confuses the initialization of the array with the initialization of the set element. The initialization of an array is the space required for the allocation of arrays, and the initialized array, where the elements are not instantiated, are still empty, so you also need to initialize each element (if it is to be called).
2. The java.lang.ClassNotFoundException exception is interpreted as "The specified class does not exist."
3. Java.lang.ArithmeticException this anomaly is interpreted as "a mathematical anomaly," such as an exception that occurs in a program divided by 0.
4. Java.lang.ArrayIndexOutOfBoundsException
The explanation of the exception is "array subscript out-of-bounds", now most of the program has an array of operations, so in the call arrays must be carefully checked, to see if the subscript is not beyond the scope of the array, in general, the display (that is, directly with the constant subscript) call is not easy to make such a mistake, But implicit (that is, the use of variable-subscript) calls are often wrong, there is also a case, the length of the array defined in the program is determined by certain methods, not in advance declaration, at this time, it is best to look at the length of the array to avoid this exception.
5. Java.lang.IllegalArgumentException
The explanation of this exception is "parameter error of the method", such as G.setcolor (int red,int green,int blue) Three values in this method, if more than 255 will also occur this exception, so once we find this anomaly, we have to do, Just go check it out. Parameter passing in the method call is not an error.
6. Java.lang.IllegalAccessException
This exception is interpreted as "no access", which occurs when the application is calling a class, but the current method does not have access to the class. Note This exception in cases where the package is used in the program (http://www.cnblogs.com/qinqinmeiren/archive/2010/10/14/2151702.html)

RuntimeException and Checkedexception

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.