Java Runtime Exceptions and non-run-time exceptions

Source: Internet
Author: User

1.Java anomaly Mechanism

Java handles exceptions as objects, and defines a base class java.lang.Throwable as a superclass of all exceptions. Exceptions in Java fall into two main categories: Error errors and exception Exception,java exception architectures as shown:


Image source: http://blog.csdn.net/wuwenxiang91322/article/details/10346337


2.Throwable

The Throwable class is a superclass of all exceptions or errors, and it has two subclasses: Error and exception, respectively, representing errors and exceptions. The exception Exception is divided into runtime exceptions (RuntimeException) and non-runtime exceptions, also known as non-check exceptions (unchecked Exception) and check exceptions (Checked Exception).


3.Error

Java Virtual machine-related problems, such as system crashes, virtual machine error, dynamic link failure, etc., this error can not be recovered or impossible to capture, will lead to application disruption, usually the application cannot handle these errors, so the application should not catch the Error object, It is also not necessary to declare in its throws clause that the method throws any error or its subclasses.


4. Run-time exceptions and non-run-time exceptions

(1) Runtime exception is RuntimeException class and its subclass exception, such as NullPointerException, indexoutofboundsexception, etc., these exceptions are not check exceptions, the program can choose to capture processing, can also not be processed. These exceptions are usually caused by a program logic error, and the program should avoid this kind of exception as much as possible from a logical point of view.

When there is a runtimeexception, we can not deal with it. When such an exception occurs, it is always taken over by the virtual machine. For example: No one has ever dealt with a nullpointerexception exception, which is a run-time exception, and this exception is one of the most common exceptions.
When a run-time exception occurs, the system throws the exception all the way to the top and continues to experience processing code. If there is no processing block, to the topmost level, if it is multithreading is thrown by Thread.run (), if it is a single thread is thrown by main (). Once thrown, the thread exits if it is a thread. If the exception is thrown by the main program, then the entire program exits. Runtime exceptions are subclasses of the exception, and there are general exceptions that can be handled by catch blocks. It's just that we're not dealing with him. That is, if you do not handle a run-time exception, then a run-time exception occurs, either the thread aborts or the main program terminates.
If you do not want to terminate, you must catch all run-time exceptions and never let the processing thread exit. Abnormal data in the queue, the normal processing should be to discard the abnormal data, and then log. The handling of normal data should not be affected by abnormal data.


(2) A non-runtime exception is an exception that is runtimeexception except that the type belongs to the exception class and its subclasses. such as IOException, SQLException , and user-defined exception exceptions. For this exception, the Java compiler enforces that we must catch and process these exceptions, otherwise the program will not compile through. So, in the face of such anomalies whether we like it or not, we can only write a lot of catch blocks to deal with possible exceptions.


Common runtimeexception:

Arraystoreexception exception thrown when attempting to store an object of the wrong type to an array of objects
ClassCastException throws this exception when attempting to cast an object to a subclass that is not an instance
IllegalArgumentException throws an exception indicating that an invalid or incorrect argument was passed to the method
Indexoutofboundsexception indicates that a sorted index (for example, sorting an array, string, or vector) is out of range when thrown
Nosuchelementexception indicates that there are no more elements in the enumeration
NullPointerException when an application attempts to use null where the object is needed, the exception is thrown


Java Runtime Exceptions and non-run-time exceptions

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.