What are the similarities and differences between Java Runtime Exceptions and general exceptions?

Source: Internet
Author: User

Turn from:

http://blog.csdn.net/rainminism/article/details/51208572

Throwable is the parent of error handling in all Java programs, and there are two types of assets: Error and exception.

Error: Indicates an unexpected fault detected by the JVM, because this is a serious error at the JVM level, which prevents the JVM from continuing, so this is not a snap and cannot take any recovery action, at most, only error messages can be displayed. The error class system describes the internal errors and resource exhaustion scenarios in the Java operating system. Applications should not throw objects of this type (typically thrown by virtual machines). In the event of such an error, there is no other way of doing anything other than trying to get the program safely out.

Exception: Represents recoverable exceptions, which can be captured.

Java provides two main types of exceptions: Runtime exception and checked exception. The checked exception is the IO exception that we often encounter, and the SQL exception is the exception. For this exception, the Java compiler enforces that we must catch the exceptions that occur. 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. Such exceptions are generally external errors, such as trying to read data from the end of a file, which is not an error in the program itself, but an external error that occurs in the application environment.

But another exception: Runtime exception, also known as runtime exception, we can not handle. 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. The runtimeexception system includes wrong type conversions, array cross-border access, and attempts to access null pointers, and so on. The principle of dealing with runtimeexception is that if runtimeexception is present, it must be a programmer's fault. For example, You can avoid array out-of-bounds access exceptions by examining array subscripts and arrays boundaries.

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. This can be a good application in this scenario, but it does not mean that you should do so in all scenarios. If, in other scenarios, you encounter some errors, if you exit the program better, then you can ignore the runtime exception, or by the exception of the handling of explicit control program exit. One of the goals of exception handling is to recover the program from the exception.

What are the similarities and differences between Java Runtime Exceptions and general 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.