Real application of anomalies

Source: Internet
Author: User

/** * 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

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.