Java Runtime Exceptions and non-run-time exceptions

Source: Internet
Author: User
Tags terminates throwable

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. Can check abnormal and non-check abnormal

In general, Java exceptions (including exception and error) are categorized as checked exceptions and non-checked exceptions (unchecked exceptions).
can check exceptions (compiler requirements must be disposed of exceptions): the correct program in operation, it is easy to appear, reasonable tolerance of abnormal conditions. Although the abnormal condition can be checked, it can be predicted to some extent, and in the event of such abnormal situation, it must be handled in some way.
in addition to RuntimeException and its subclasses, other exception classes and their subclasses belong to the exception-checking. This exception is characterized by the Java compiler checking it , that is, when such an exception can occur in a program, either by capturing it with a try-catch statement or by declaring it with a throws clause, or the compilation will not pass.
Exception not checked (compiler does not require forced disposition of exceptions): Includes run-time exceptions (RuntimeException with its subclasses) and errors (error).

If you throw a check exception in the method body using throw, you need to declare the type of exception that the method might throw in the method header. The program terminates immediately after the throw statement, and the statement following it is not executed, and then in all the try blocks that contain it (possibly in the upper call function), look for the try block containing the catch clause that matches it.

5. 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.
After a run-time exception occurs, if there is no catch to handle the exception (that is, there is no catch), the system will always throw the exception to the upper level, if it is multi-threaded by Thread.run () thrown, 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.

6.finally Keywords

Take a look at the following test1 () method:

 Public int test1 () {          try  {              return 1;           finally {              return 2;          }      

Method Test1 will return 2;

How do you explain it? Take a look at the following test2 () method:

 Public int test2 () {          int i = 1;           Try {              System.out.println (in "try statement block");               return 1;           finally {              System.out.println (in "finally statement block");               return 2;          }        }  

The operating result is:

In a try statement block
In the finally statement block
2

It can be found from the running result that the function called by the return statement in the try is executed before the function called in Finally, that is, the return statement executes first, and the finally statement executes , so the returned result is 2. Return does not return the function immediately, but the return statement executes, and the returned result is placed in the function stack, and the function does not return immediately, and it does not actually begin to return until the finally statement is executed.


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

Reference: http://blog.csdn.net/wuwenxiang91322/article/details/10346337

Java Runtime Exceptions and non-run-time exceptions

Related Article

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.