Java exception loss and exception chaining

Source: Internet
Author: User

Case of abnormal loss:

Define three exceptions first:

 Public classExceptionaextendsException { PublicExceptiona (String str) {Super(); }}  Public classExceptionbextendsExceptiona { Publicexceptionb (String str) {Super(str); }}  Public classExceptioncextendsExceptiona { Publicexceptionc (String str) {Super(str); }}

Then define a test class:

 Public classNevercaught {Static voidF ()throwsexceptionb{Throw NewEXCEPTIONB ("Exception B"); }     Static voidG ()throwsExceptionc {Try{f (); } Catch(exceptionb e) {exceptionc C=NewExceptionc ("Exception a"); ThrowC; }    }      Public Static voidMain (string[] args) {Try{g (); } Catch(Exceptionc e) {e.printstacktrace (); }    } }

Results of the output:

exception. Exceptionc
At exception. NEVERCAUGHT.G (Nevercaught.java:12)
At exception. Nevercaught.main (nevercaught.java:19)

The EXCEPTIONB information was lost in the printed exception message. If you want to be able to output exceptionb and exceptionc information at the same time, you need to use an exception chain.

The main function of the exception chain is to save the exception information, while throwing another exception without losing the original exception.

The following code implements an exception chain up-throws an exception in two ways.

 Public classNevercaught {Static voidF ()throwsexceptionb{Throw NewEXCEPTIONB ("Exception B"); }     Static voidG ()throwsExceptionc {Try{f (); } Catch(exceptionb e) {exceptionc C=NewExceptionc ("Exception a"); //Exception ChainC.initcause (e); //another way to achieve this//throw new Exception ("Exception a", e);             ThrowC; }    }      Public Static voidMain (string[] args) {Try{g (); } Catch(Exceptionc e) {e.printstacktrace (); }    } }

It is important to note that:

Only Error,exception,runimeexception provides a constructor with the cause parameter, and so the exception is set cause only by Initcause (). When you define an exception that provides a constructor with the cause parameter, it can also be set normally.

Java exception loss and exception chaining

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.