Unusual exercises:
The teacher uses the computer for class.
Start thinking about the problems that arise in class.
For example, the problem is
Computer blue screen.
The computer smokes.
To describe the problem, encapsulate it as an object.
However, when the smoke occurred, the lecture progress could not continue.
The instructor's question arose: The lesson plan could not be completed.
1 classTeacher2 {3 PrivateComputer CMP;4 Public voidShangke ()throwsNoplanexception/*declaring exceptions*/5 {6cmp=Newcomputer ();7 Try8 {9 Cmp.run ();Ten } One Catch(Lanpingexception e)/*computer captures exceptions that handle blue screens*/ A { - cmp.recst (); - } the Catch(Maoyanexception e)/*computer capture to handle computer smoke anomalies*/ - { - Throw NewNoplanexception ("cannot continue in class because" +e.getmessage ());/*The computer can't handle this exception, continue to throw this exception to the teacher to handle*/ - } + -System.out.println ("Teacher Class");/*no abnormalities, the teacher is normal class*/ + } A } at classLanpingexceptionextendsException/*Custom blue screen exceptions*/ - { - lanpingexception (String m) - { - Super(m); - } in } - to classMaoyanexceptionextendsException/*Custom Computer Smoke Exceptions*/ + { - maoyanexception (String m) the { * Super(m); $ }Panax Notoginseng } - classNoplanexceptionextendsException/*Custom Teacher Handling Exceptions*/ the { + noplanexception (String m) A { the Super(m); + } - } $ $ classComputer - { - Private intstate=3;/*different exception State selection*/ the - Public voidRun ()throwslanpingexception,maoyanexceptionWuyi { the if(state==2) - { Wu Throw NewLanpingexception ("Computer blue screen");/*throws an exception object if it meets the criteria*/ - } About if(state==3) $ { - Throw NewMaoyanexception ("The computer is smoking"); - } -System.out.println ("PC Run"); A } + the - Public voidRECST () $ { theSYSTEM.OUT.PRINTLN ("PC Restart"); the } the } the - classExceptiontext in { the Public Static voidMain (String args[]) the { AboutTeacher t=NewTeacher (); the Try the { the T.shangke (); + } - Catch(Noplanexception e)/*teacher caught handling computer smoking abnormality*/ the {Bayi System.out.println (e.tostring ()); the } the } -}
Operation Result:
Noplanexception: The class can't continue because the computer is smoking
Class Teacher
{
Private computer CMP;
public void Shangke () throws noplanexception/* Declaration Exception */
{
Cmp=new computer ();
Try
{
Cmp.run ();
}
catch (lanpingexception e)/* Computer captures exception handling blue screen */
{
CMP.RECST ();
}
catch (maoyanexception e)/* Computer captures exception handling computer smoke */
{
throw new Noplanexception ("cannot continue in class because" +e.getmessage ()); /* The computer is unable to handle this exception, continue to throw this exception to the teacher to handle * *
}
System.out.println ("Teacher Class"); /* No abnormalities, the teacher's 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 Handling 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"); /* Throws the exception object if it meets the criteria */
}
if (state==3)
{
throw new Maoyanexception ("The computer Smokes");
}
SYSTEM.OUT.PRINTLN ("Computer Run");
}
public void Recst ()
{
SYSTEM.OUT.PRINTLN ("computer restart");
}
}
Class Exceptiontext
{
public static void Main (String args[])
{
Teacher t=new Teacher ();
Try
{
T.shangke ();
}
catch (noplanexception e)/* teacher captures handle computer smoking abnormality */
{
System.out.println (E.tostring ());
}
}
}
Java Exception Practice