Checked and unchecked exception

Source: Internet
Author: User

I,

Java defines two types of exceptions:

1) Checked exception: these exceptions are subclasses of exception. Exception is thrown up. If the subclass may produce a exception, throws a must also be thrown in the parent class. Possible problems: Low code efficiency and high coupling. C # does not use this exception mechanism.

2) unchecked exception: these exceptions are all subclasses of runtimeexception. Although runtimeexception is also a subclass of exception, they are special and cannot be solved through client code. Therefore, they are called unchecked exception.
 
In addition:
 
Error indicates that recovery is not a serious problem that is impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such a situation.

Exception indicates a design or implementation problem. That is to say, it indicates that if the program runs normally, it will never happen.

II,

Checked exception is an exception that requires forced catch. When you call this method, if you do not catch this exception, the compiler will report an error. For example, when we read and write files, we will catch ioexception, sqlexception will occur when performing database operations
Unchecked exception is a runtimeexception, that is, an exception during running. This exception does not have to be caught and you cannot predict it. For example, you are calling a list. when szie () is used, if this list is null, nullpointerexception will be reported, and this exception is runtimeexception, that is, unchecked exception.
 
III,
 
Error, runtimeexception, and its subclass are unchecked exception. Other exceptions are checked exception.
Checked exception can appear in the throws clause, but unchecked exception cannot.
Errors are Java errors or serious errors, such as memory depletion, which are irresistible. Obviously there is no need to capture them, and there is no way to catch them.
Runtimeexception is a logical error in your program. It is an exception that programmers should actively avoid. For example, nullpointerexception and so on are all errors made by programmers. When such an error occurs, Java automatically captures the error, such as displaying it in concole, and continues running. If the checked exception is not captured, the program will be terminated.
 
Iv. Differences between error and excption
 
Inheritance relationship of error:
 
Java. Lang. Object

-- Java. Lang. throwable
 
-- Java. Lang. Error
 
The inheritance relationship of exception:
 
Java. Lang. Object
 
-- Java. Lang. throwable
 
-- Java. Lang. Exception
 
Differences between the two:
 
Exception:
 
1. It can be controllable (checked) or unchecked)
 
2. indicate an error caused by a programmer
 
3. It should be processed at the application level
 
Error:
 
1. Always unchecked)
 
2. It is often used to indicate system errors or low-level resource errors.
 
3. How can this problem be captured at the system level?

From: http://blog.csdn.net/yuefengyuan/archive/2011/02/24/6204317.aspx

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.