try catch a custom exception is caught

First, create a MyException class that inherits from the Exception class

Code is implemented as follows:

Inherited from the Exception class, parent class called MyException method, return an error message.

1 public class MyException extends Exception {
2 
3     public MyException(String message) {
4         super(message);
5     }
6 }

Determine whether the use of such a character entry is a positive integer types, if not, return the associated error; otherwise it returns rating results in accordance with the relevant conditions.

When using try catch, the generated code in the exception throw statement may be captured and catch statement. (It must be thrown in order to be captured)

There may be an exception must be placed inside the try code block, in order to capture the possible exceptions thrown. Try a class catch may correspond to one or more classes, but an exception is thrown capture a catch, the other will not be executed.

. 1  Import java.util.Scanner;
 2  
. 3  public  class Students. {
 . 4      static Scanner SC = null ;
 . 5      
. 6      static  int Check (String C) throws MyException {
 . 7          IF !? (C.matches ( "\\ ^ - [0- 9] + $ ")) {   // matches all integers 
. 8              the throw  new new MyException (" Please enter an integer number "! );
 9          }
 10          IF ((Integer.valueOf (C))> 100 || (Integer.valueOf ( C)) <0 ) {   
 . 11           the throw  new new MyException ( "Please enter an integer between 0 and 100!" );
12         }
13         
14         return Integer.valueOf(c);
15     }
16     
17     public static void main(String[] args)  {
18         System.out.println("请输出一个整数:");
19         String c=null;
20         
21         sc=new Scanner(System.in);
22         c=sc.nextLine();
23         try {
24         int a=check(c);
25         if(a<=100&&a>=90) {
26             System.out.println("优!");
27         } 
28         else if(a>=80&&a<90) {
29             System.out.println("良!");
30         } 
31         else if(a>=70&&a<80){
32             System.out.println("中!");
33         }
34         else if(a>=60&&a<70) {
35         System.out.println("及格!");
36         }
37         else{
38             System.out.println ( "fail!" );
39         }
40         }
41         catch (MyException e) {
42             System.out.println(e);
43         }
44 
45 }
46 }

Screenshot program as follows:

 

 

 

 

 

 

 In addition, the complete statement also try catch finally statement, which usually perform, will not only perform in the following ways:

1. abnormality occurs in the finally block.

2. Use of System.exit (0) in the preceding code statements

3. The program is located thread death

4. Close cpu

Join finally statement runs as follows:

. 1  Import java.util.Scanner;
 2  
. 3  public  class Students. {
 . 4      static Scanner SC = null ;
 . 5      
. 6      static  int Check (String C) throws MyException {
 . 7          IF !? (C.matches ( "\\ ^ - [0- 9] + $ ")) {   // matches all integers 
. 8              the throw  new new MyException (" Please enter an integer number "! );
 9          }
 10          IF ((Integer.valueOf (C))> 100 || (Integer.valueOf ( C)) <0) {   // determines whether or not a predetermined range of an integer 
. 11           the throw  new newMyException ( "Please enter an integer between 0 and 100!" );
 12 is          }
 13 is          
14          return Integer.valueOf (C);
 15      }
 16      
. 17      public  static  void main (String [] args) {
 18 is          System.out.println ( "Please output an integer:" );
 . 19          String C = null ;
 20 is          
21 is          SC = new new Scanner (the System.in);
 22 is          C = sc.nextLine ();
 23 is          the try {
 24          int a = Check (C);
 25          IF (A <= 100 && A> = 90 ) {
26             System.out.println("优!");
27         } 
28         else if(a>=80&&a<90) {
29             System.out.println("良!");
30         } 
31         else if(a>=70&&a<80){
32             System.out.println("中!");
33         }
34         else if(a>=60&&a<70) {
35         System.out.println("及格!");
36         }
37         else{
38             System.out.println ( "fail!");
 39          }
 40          }
 41 is          the catch (MyException E) {
 42 is              System.out.println (E);   // capture the output of the abnormality information 
43 is          }
 44 is     the finally {
 45         System.out.println ( "abnormal completion check!");   // perform a finally block 
46     }
 47  }
 48 }

Do the following:

 

 

 

Guess you like

Origin www.cnblogs.com/cxy0210/p/11761861.html