A detailed example of the java exception

  • 2020-10-23 20:58:41
  • OfStack

A detailed example of the java exception

1. Definition of exception: Abnormal situation occurs when the program is running.

Division of exceptions:

Error: Serious problems that are handled by error1 without specific code.

Exception: Not a serious problem. exception can be treated in a targeted way.

2. Handling of exceptions :(fixed format)


try

 {code that needs to be detected; } 

catch (exception class   Variables) 

 {code to handle exceptions (handling mode); } // This should be a targeted approach 

finally

 { 1 A statement that must be executed; } // Usually the code that closes the resource, because the resource must be freed 

How to handle multiple exceptions:

1. When declaring an exception, it is recommended to declare a more specific exception so that it can be handled more specifically.

2, the other side declared a few exceptions, corresponding to pass catch block, do not define redundant catch block, if the exception in multiple catch block inheritance relationship, the parent exception catch block placed at the bottom.

* When catch processing is carried out, catch 1 must define the specific processing method.

*catch is used to handle exceptions. If there is no catch, it means that the exception has not been handled. If the exception is a detected exception, then it must be declared.

Runtime exceptions :(that is, runtime exceptions)

Is a special subclass exception in Exception.

If the exception is thrown in the function, the function can not be declared, compile 1 sample passed; If the exception is declared on the function, the caller does not need to handle it, and the compile 1 sample passes.

This is used for function declarations because you don't need to let the caller process them.

The representation of exception in child superclass coverage;

1. When a subclass overrides a parent class, if a method of the parent class throws an exception, then the overriding method of the subclass can only throw an exception of the parent class or a subclass of the exception.
2. If a superclass method throws more than one exception, a subclass can only throw a subset of the superclass exception when overwriting the method.
3. If no exception is thrown in a method of the parent class or interface, a subclass cannot throw an exception when overwriting the method.

If an exception occurs in a subclass method. You have to do the try processing. Absolutely not.

Differences between throws and throw:

throws: Used on functions (between curly braces and curly braces), followed by an exception class, which can be followed by more than one. Put a comma between them.

throw: Used within functions, followed by exception objects. throw cannot be followed by a statement because the statement following throw cannot be executed to.

Exercises:


/* Requirement: Mr. Bi USES computer for class.  
 Analysis:  
 First write the normal operation of the code: the computer turned on after class  
 Analyze computer problems, such as: blue screen, smoke  
 At this point you need to customize two "blue screen, smoke" custom exceptions, encapsulated into objects.  
 After the blue screen, the computer was rebooted and the class resumed  
 After the computer smoked, there was a problem with the teacher. He could not attend the class, so he could do some exercises first. Mr. Bi threw the problem to the principal. The problem was no longer that the computer was smoking, but that the teacher could not attend the class  
 The question was thrown to the principal, the principal catch Then the solution is given  
*/ 

class LanPingException extends Exception // Custom blue screen exception  
{ 
  LanPingException(String message) 
  { 
    super(message); 
  } 
} 
 
class MaoYanException extends Exception// Custom smoke exception  
{ 
  MaoYanException(String message) 
  { 
    super(message); 
  } 
} 
 
class NoclassException extends Exception// Custom cannot attend class exception  
{ 
  NoclassException(String message) 
  { 
    super(message); 
  } 
} 
 
class Computer  
{ 
  private int state = 2;// Set the state of the computer  
  // The problem occurs at runtime, so it has to be defined at runtime  
  public void run()throws LanPingException,MaoYanException// Because errors are possible, you must declare them first  
  { 
    if (state==2) 
    { 
      throw new LanPingException(" The computer is blue... ");// The computer couldn't handle it by itself, so it was thrown to the teacher  
    } 
    if (state==3) 
    { 
      throw new MaoYanException(" The computer is smoking... "); 
    } 
    System.out.println(" The computer is on, running... ");  
  } 
  public void restart() 
  { 
    System.out.println(" Reboot the computer... "); 
  } 
} 
class Teacher 
{ 
  private String name; 
  private Computer comp; 
  Teacher(String name)  
  { 
    this.name = name; 
    comp = new Computer();// The teacher will have a computer when it's initialized  
  } 
  public void test() 
  { 
    System.out.println(" Do exercise "); 
  } 
  public void teach() throws NoclassException// Flags should be thrown exceptions that can be handled  
  { 
    try 
    { 
      comp.run();// In the lecture, turn on the computer first  
    } 
    catch (LanPingException e)// Two exceptions were thrown, and two must be used catch 
    { 
       
      System.out.println(e.getMessage()); 
      comp.restart(); 
    } 
    // If this problem is thrown to the teacher, it still cannot be solved, so the corresponding problem should be thrown. In this case, the problem thrown by the teacher should be unable to attend class  
    catch (MaoYanException e) 
    { 
      test();// Can't attend class, can assign exercise first, this must be in throw  Before, because throw Subsequent statements are not executed  
      throw new NoclassException(e.getMessage()+", Unable to continue class "); 
    } 
     
    System.out.println(name+", In class... "); 
  } 
} 
 
class ExceptionTest 
{ 
  public static void main(String[] args) 
  { 
    Teacher t = new Teacher(" Never put off till tomorrow what you can the teacher ");// The specified 1 A teacher  
    try 
    { 
       
      t.teach();// The teacher runs this method  
    } 
    catch (NoclassException e)// Catch the corresponding question thrown  
    { 
      System.out.println(e.toString());// Print out the cause of the problem  
      System.out.println(" Study or vacation ");// The solution  
    } 
     
  } 
} 

 When the computer state is zero 1 , the output is:  
 The computer is on, running...  
 Never put off till tomorrow what you can the teacher , In class...  
 When the computer state is zero 2 , the output is:  
 The computer is blue...  
 Reboot the computer...  
 Never put off till tomorrow what you can the teacher , In class...  
 When the computer state is zero 3 , the output is:  
 Do exercise  
NoclassException:  The computer is smoking... , Unable to continue class  
 Study or vacation  

If you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: