Java Custom Exceptions

Source: Internet
Author: User

When appropriate, replace the if else with a try Catch

 Packagecom.test;/**  * @authorQiaojiafei *@versioncreated: January 6, 2016 2:58:41 * class Description*/ Public classTestException { Public Static voidMain (string[] args) {//TODO auto-generated Method StubTeacher T =NewTeacher ("Cho Jiafei"); Try{T.JK (); } Catch(noplanexception e) {System.out.println (e.tostring ()+".............."); //TODO auto-generated Catch blockSystem.out.println ("I know.")); }    }}classTeacher {String name;    Computor CP;  PublicTeacher (String name) {//TODO auto-generated Constructor stub         This. Name =name; CP=NewComputor (); }        voidJK ()throwsnoplanexception {Try{cp.run (); SYSTEM.OUT.PRINTLN (Name+ "Start Lecture"); } Catch(maoyanexception e) {//TODO auto-generated Catch blockSystem.out.println (e.tostring ());            Test (); Throw  NewNoplanexception ("Stop the lecture, the reason is:" +e.tostring ()); } Catch(lanpingexception e) {//TODO auto-generated Catch blockSystem.out.println (e.tostring ());            Cp.reset (); Cp.state= 0;        JK (); }    }        voidTest () {System.out.println ("Contact Us"); }    }classComputor {intState = 2; voidRun ()throwsmaoyanexception, lanpingexception {if(state = = 1)            Throw NewLanpingexception ("Computer blue screen"); if(state = = 2)            Throw NewMaoyanexception ("The computer is smoking"); System.out.println ("PC starts running"); }        voidReset () {System.out.println ("Computer Restart"); }}classMaoyanexceptionextendsException {maoyanexception (String msg) {Super(msg); }}classLanpingexceptionextendsException {lanpingexception (String msg) {Super(msg); }}classNoplanexceptionextendsException {noplanexception (String msg) {Super(msg); }}

Java Custom Exceptions

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.