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