The simple practice of Java exception handling _java

Source: Internet
Author: User

Unusual exercises:

The teacher uses a computer for class.

Start thinking about the problems that arise in class.

For example, the question is

Computer blue screen.

The computer is smoking.

To describe the problem, encapsulate it as an object.

But when the smoke occurs, the progress of the lectures cannot continue.

There was a problem with the instructor: The lesson plan could not be completed.

Class Teacher {private Computer cmp;
    public void Shangke () throws noplanexception/* Declaration Exception * * Cmp=new Computer ();
    try {cmp.run ();
    catch (lanpingexception e)/* Computer capture processing blue Screen Exception * * {CMP.RECST (); catch (maoyanexception e)/* Computer capture processing Computer Smoke exception/{throw new Noplanexception ("Class cannot continue, because" +e.getmes    Sage ());                * * The computer can not handle this exception, continue to throw this exception to the teacher to deal with/} System.out.println ("Teacher Class");
  /* No exception, the teacher on the normal class/} class Lanpingexception extends Exception/* Custom BLUE Screen Exception * * Lanpingexception (String m)
  {super (M);
  } class Maoyanexception extends Exception/* Custom Computer Smoke exception/{maoyanexception (String m) {super (M);
  } class Noplanexception extends Exception/* Custom Teacher handles exception * {noplanexception (String m) {super (M);           } class Computer {private int state=3; /* Different exception State selection */public void run () throws Lanpingexception,maoyanexception {if (state==2) {throw new Lanpingexception ("Computer blue screen");
    /* Meet the criteria to throw the exception object */} if (state==3) {throw new Maoyanexception ("Computer Smoke");
  } System.out.println ("Computer Run");
  public void Recst () {System.out.println ("computer reboot");
    } class Exceptiontext {public static void main (String args[]) {Teacher t=new Teacher ();
    try {T.shangke ();
    catch (noplanexception e)/* Teacher capture processing computer smoke anomaly/{System.out.println (e.tostring ()); }    
  }
}

Run Result:

Noplanexception: The class can't go on because the computer is smoking.

The above Java exception handling of the simple exercise is small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.