1 classLanpingexceptionextendsException2 {3 lanpingexception (String msg)4 {5 Super(msg);6 }7 }8 9 classMaoyanexceptionextendsExceptionTen { One maoyanexception (String msg) A { - Super(msg); - } the } - - classComputer - { + Private intstate=2; - Public voidRun ()throwslanpingexception,maoyanexception + { A if(state==1) at { - Throw NewLanpingexception ("Lanping!!!"); - } - if(state==2) - { - Throw NewMaoyanexception ("Maoyan!!!"); in } -System.out.println ("Run Bat"); to } + Public voidReset () - { theState=0; *SYSTEM.OUT.PRINTLN ("Computer reset!"); $ }Panax Notoginseng } - the classTeacher + { A PrivateString name; the Privatecomputer comp; + Teacher (String name) - { $ This. name=name; $comp=Newcomputer (); - } - Public voidPrelect ()throwsmaoyanexception the { - TryWuyi { the Comp.run (); -System.out.println (name+ "Speak"); Wu } - Catch(lanpingexception e) About { $ System.out.println (e.tostring ()); - Comp.reset (); - Prelect (); - } A Catch(maoyanexception e) + { the System.out.println (e.tostring ()); - test (); $ Throwe; the } the } the Public voidTest () the { -System.out.println ("Test yourself!"); in } the } the About classKandra the { the Public Static voidMain (string[] args) the { +Teacher pp=NewTeacher ("Cao"); - Try the {Bayi Pp.prelect (); the the } - Catch(maoyanexception e) - { theSystem.out.println ("..."); the } the } the}
Java exception throw and Try,catch application instance