Java exception-Check exception (checked exception) and unchecked exception (unchecked exception)

Source: Internet
Author: User

NetEase interview to me to draw unusual structure diagram, what is check abnormal, what is abnormal check, my expression at that time is such,. I've seen it, forget it. No way, continue to read, write blog master.

Let's take a look at the unusual structure diagram and suggest you look at it together with the JDK.


It can be seen that the abnormal family power is huge, and usually we say that the anomaly is including exceptio and error.

Exception family We are afraid to see a lot, but the error family we may not be very impressed, below I say the difference between the two classes:

Error (Error): A bug that the program cannot handle, indicating a more serious problem in running the application. Most of the errors are not related to what the code writer does, but rather the problems that occur with the JVM (the Java virtual machine) when the code runs. For example, the Java Virtual machine runs the error (virtual Machineerror) and OutOfMemoryError occurs when the JVM no longer has the memory resources required to continue the operation. When these exceptions occur, the Java Virtual machine (JVM) typically chooses to terminate the thread. These errors indicate that the failure occurred on the virtual machine itself, or when the virtual machine attempted to execute the application, such as a Java Virtual machine run error (virtual Machineerror), a class definition error (NOCLASSDEFFOUNDERROR), and so on. These errors are not available because they are outside the control and processing power of the application and are mostly not allowed when the program is running. For a well-designed application, even if it does, it should not be an attempt to deal with the anomalies that it causes. In Java, errors are described by the subclasses of the error.

Personal Understanding: error errors are generally not due to the code you write problems, but because the JVM running your code is a problem, such as heap overflow, configuration file errors, etc., you can not be artificial in your code to ensure that you have to be extra to operate, Reconfigure the virtual machine, modify the configuration file, and so on.

Exception (Exception): is an exception that the program itself can handle. This is your common null pointer exception (nullpointerexception), array out of range exception (indexoutofboundsexception), and so on.

Typically, Java exceptions (including exception and error) are divided into check exceptions (checked exceptions) and non-checked exceptions (unchecked exceptions).

I. Check for anomalies (checked exceptions)

What is a check exception?

A: This is the exception that the compiler requires you to dispose of. Do not know when you are programmed to encounter, you write a piece of code, the compiler requires that you have to try...catch this code, or throws exception, if you have met, yes, this is to check the exception, that is, your code is not running yet, The compiler will check your code, and there will be exceptions that require you to handle the exceptions that might occur.

Several ways to handle check exceptions (checked exception):
1, continue to throw, the negative method, can always be thrown into the Java virtual machine to deal with, that is, by throws exception thrown.
2. Capture with Try...catch
Note that the exception that is checked must be handled, or must be caught or must be thrown

How do you differentiate between checking exceptions?

In addition to RuntimeException and its subclasses, as well as errors (error), the others are check exceptions (absolute large families).

Second, non-check exception (unchecked exceptions)

What is a non-check exception?

A: The compiler does not require a forced disposition of the exception, although you may have an error, but I will not be at compile time check, no need, also impossible. Why is it? What do you think about non-check exceptions? Nullpointerexception,indexoutofboundsexception,virtualmachineerror Wait, are these exceptions checked when you compile? Then I'm not going to run, wait for the dead. Besides, obviously can run the check, all at compile time check, you write code still can see? And some exceptions can only be checked at runtime, such as null pointers, heap overflows, and so on.

Several ways to handle an unchecked exception (unchecked exception):
1. Capture
2. Continue to throw
3, do not handle

Generally we do not deal with, because you are very difficult to determine what the problem, and some exceptions you can not run the processing, such as null pointers, people need to manually find.

Moreover, catching exceptions and handling them is much more costly than outright throwing.

How do you differentiate between non-check exceptions?

RuntimeException with its subclasses, as well as errors (error).

By the way, run-time exceptions and non-run-time exceptions.

Just now we are dividing from exception and error as a whole, so we can divide the exception exception, which can be divided into runtime exception and non-runtime exception.

One, run-time exception

are runtimeexception classes and their subclass exceptions, such as nullpointerexception (null pointer exception), indexoutofboundsexception (subscript out-of-bounds exception), and so on, which are non-check exceptions, You can choose to capture or not handle the program. 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.

A run-time exception is characterized by the Java compiler not checking it, that is, when such an exception can occur in a program, even if it is not captured with the Try-catch statement, it is not thrown with the throws clause declaration, and it is compiled.

Second, non-run-time exceptions

is an exception other than RuntimeException, and the type belongs to the exception class and its subclasses. From the point of view of the program syntax is the exception that must be handled, and if not handled, the program cannot be compiled through. such as IOException, SqlException, and user-defined exception exceptions, generally do not customize check exceptions.

Reference documents:

In-depth understanding of Java exception handling mechanisms

Check for exceptions and non-checked exceptions

Java exception-Check exception (checked exception) and unchecked exception (unchecked exception)

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.