Java section 50th, the exception chain in Java

Source: Internet
Author: User

2016-06-30

1 Abnormal chain
Two or more different exceptions appear in the same program, and nested throws occur, which we call the exception chain.

 Packagecom.java1995;/*** Abnormal chain *@authorAdministrator **/ Public classExceptionchaintest { Public Static voidMain (string[] args) {Calculator C=NewCalculator (); Try{C.chufa (1, 0); }Catch(numbercalculateexception e) {e.printstacktrace (); System.out.println ("Cause of error" +e);    }; }}classcalculator{/*** Division *@return     * @throwsnumbercalculateexception*/     Public intChufa (intIintJthrowsnumbercalculateexception{if(j==0) {numbercalculateexception e=NewNumbercalculateexception ("Calculation Error"); Negativenumberexception E1=NewNegativenumberexception ("Divisor cannot be 0");            E.initcause (E1); Throwe; }        return0; };}classNegativenumberexceptionextendsException { Publicnegativenumberexception (String msg) {Super(msg); }}classNumbercalculateexceptionextendsException { Publicnumbercalculateexception (String msg) {Super(msg); }}

Resources

[1] Java Easy Start Classic tutorial "full version"

Java section 50th, the exception chain in Java

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.