Java section 49th manually throwing exceptions

Source: Internet
Author: User
Tags finally block throw exception try catch

2016-06-30

1 Throwing Exceptions manually
Throw exception; The parameter exception represents an object to throw, which is the Throwable class
Subclass, and can only be one.

2 Try Catch finally can be used in a nested set.

 Packagecom.java1995;/*** 1 Throwing Exceptions manually *@authorAdministrator **/ Public classThrowtest { Public Static voidMain (string[] args) {throwtest T=Newthrowtest (); Try{t.test (); } Catch(Exception e) {//TODO auto-generated Catch blockSystem.out.println (E.getmessage ()); }    }         Public voidTest ()throwsexception{Throw NewException ("This is hand-thrown"); }}

 Packagecom.java1995;/*** 2 try...catch...finally nested * Many more encountered in database connection pool *@authorAdministrator **/ Public classTrycatchfinallytest { Public Static voidMain (string[] args) {DoubleA=Math.random (); Try{            if(a>0.5) {System.out.println (a+ ", program does not error"); }Else{                Throw NewException (); }                    }Catch(Exception e) {System.out.println ("This is the object captured by the outer layer" +e); Try{a=1/0; }Catch(ArithmeticException E1) {System.out.println ("This is the object captured by the inner layer" +E1); }finally{System.out.println ("This is the finally block of the inner layer"); }        }finally{System.out.println ("This is the outer finally block."); }            }}

Resources

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

Java section 49th manually throwing 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.