Java-checked and non-checked exceptions

Source: Internet
Author: User

1. Code

 Public classExceptest {/**     * @paramargs*/     Public Static voidMain (string[] args) {System.err.println ("111111111111");//say (); //throws an exception, but the code does not display the try:        Catch.. //Calc ();//throws an exception, but the code does not display the try:        Catch.. //try {//Show ();//} catch (Exception e) {//e.printstacktrace ();//System.err.println ("Exception successfully captured: ");//        }        //try {//view ();//} catch (Exception e) {//e.printstacktrace ();//System.err.println ("Exception successfully captured: ");//        }Calc22 (); System.err.println ("222222222222"); }         Public Static voidSay () {//New NullPointerExceptionSystem.err.println ("say>>>>"); Throw NewNullPointerException (); }         Public Static voidCalc () {//throws ArithmeticException, this exception is not prompted. System.err.println ("calc>>>>"); inti = 0, j = 0; System.err.println (i/j); }         Public Static voidShow ()throwsexception{//this exception prompt. System.err.println ("show>>>>"); inti = 0, j = 0; System.err.println (i/j); }         Public Static voidView ()throwsexception{System.err.println ("View>>>>"); Throw NewException (); }         Public Static voidCalc22 () {System.err.println ("Calc22>>>>"); Try {            inti = 0, j = 0; intK = i/J; } Catch(Exception e) {System.err.println ("Exception handling completed ...." ");//throw E; //The exception is thrown again. //throw new ArithmeticException (); //inherited from exception is a check-type exception (of course, runtimeexception itself is also a subclass of exception//throw new RuntimeException (); //(of course, runtimeexception itself is also a subclass of exception//Non-check exceptions that inherit from RuntimeException or error//ArithmeticException Inheritance RuntimeException} System.err.println ("Calc22>>>>calc22"); }}

2. Description
Using spring will inevitably use spring's transaction management, and it is natural to use transaction management to choose Declarative transaction management, in the spring documentation, spring declarative transaction management defaults to non-checked exceptions and runtime exceptions for transaction rollback, A rollback operation is not performed on a check-type exception.
So what is a check-type exception and what is a non-check exception?
The simplest point of judgment is two:
1. A non-check exception that inherits from RuntimeException or error, and inherited from exception is a check-type exception (of course, runtimeexception itself is also a subclass of exception).
2. The non-check type exception can not be captured, and the check type exception must be handled with a try statement block or the exception to the superior method processing, in short, must write code to deal with it.
Reference: http://blog.csdn.net/yixiaoqingyuz/article/details/4485606

Java-checked and non-checked 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.