/** * Requirements: Teachers with computer lectures on blue screen exception and the death of abnormal machine processing * * @author Administrator * */class teacher{private String name;private Computer Com;publ IC Teacher (String name) {this.name=name;com=new computer ();} public void Teach () throws Noplanexception{try{com.run (); System.out.println (name+ "Start class"); }catch (lanpingexception e) {System.out.println (e.tostring ()); Com.reset ();//Call restart method teach ();} catch (Sijiexception e) {System.out.println (e.tostring ()), test (),//Call break method throw new Noplanexception ("The task is not finished");// Throws a method that cannot complete the task to the caller}}public void Test () {//Rest method System.out.println ("Everyone Breaks");} Class Computer{int status= (int) (Math.random ());//define state variables to get random numbers, 0 for normal, 1 for blue screen exceptions, 2 for dead exceptions, public void run () throws Lanpingexception,sijiexception{if (status==1) {//different cases throw different exceptions throw new Lanpingexception ("Your computer is blue screen");} if (status==2) {throw new Sijiexception ("Your computer has broken down!") ");} SYSTEM.OUT.PRINTLN ("Computer Run");} public void Reset () {System.out.println ("Computer restarts!") "); status=0;//restart after computer normal}}class lanpingexception extends exception{//blue screen exception lanpingexception (String s) {super (s);}} Class Sijiexception Extends exception{//computer does not use exception sijiexception (String s) {super (s);}} Class Noplanexception extends exception{//task fails to finish the exception noplanexception (String s) {super (s);}} public class Exceptionapplication {public static void main (string[] args) {Teacher t=new Teacher ("Miss Li"); try{//gets the exception that the task is not finished, and then processes the T.teach (); }catch (noplanexception e) {System.out.println (e.tostring ()); System.out.println ("Change a teacher"); }}}
If status is 0,
Computer operation
Teacher Li starts class
If status is 1
Lanpingexception: Your computer is blue screen.
Restart your computer!
Computer operation
Teacher Li starts class
If status is 2
Sijiexception: Your computer has broken down!
Everyone rest
Noplanexception: Mission Accomplished
Change a teacher
Real application of anomalies