java exception handling homework

1, hands-on brain

 

 

Source

import javax.swing.*;
class AboutException {
   public static void main(String[] a)
   {
      int i=1, j=0, k;
      k=i/j;

 try
 {
  
  k = i/j;    // Causes division-by-zero exception
  //throw new Exception("Hello.Exception!");
 }
 
 catch ( ArithmeticException e)
 {
  System.out.println("被0除.  "+ e.getMessage());
 }
 
 catch (Exception e)
 {
  if (e instanceof ArithmeticException)
   System.out.println("被0除");
  else
  { 
   System.out.println(e.getMessage());
   
  }
 }
 
 finally
     {
       JOptionPane.showConfirmDialog(null,"OK");
     }

  }
}
 
result

 

 The java error code may occur into the try block, catch statement block of code to handle errors, regardless of whether an exception occurs, finally statement block statement is always guaranteed to be executed. If no appropriate exception handling code, JVM will end out the entire application.

 

2, hands-on brain

 

 Source

public class CatchWho {
    public static void main(String[] args) {
        try {
             try {
                 throw new ArrayIndexOutOfBoundsException();
             }
             catch(ArrayIndexOutOfBoundsException e) {
                 System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");
             }
 
            throw new ArithmeticException();
        }
        catch(ArithmeticException e) {
            System.out.println("发生ArithmeticException");
        }
        catch(ArrayIndexOutOfBoundsException e) {
           System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");
        }
    }
}

result

ArrayIndexOutOfBoundsException / inner try-catch
occurs ArithmeticException

 

 

 Source

public class CatchWho2 {
    public static void main(String[] args) {
        try {
             try {
                 throw new ArrayIndexOutOfBoundsException();
             }
             catch(ArithmeticException e) {
                 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
             }
            throw new ArithmeticException();
        }
        catch(ArithmeticException e) {
            System.out.println("发生ArithmeticException");
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
        }
    }
}

result

ArrayIndexOutOfBoundsException/外层try-catch

 

3, hands-on brain

 

 Source


public class EmbededFinally {
   
 public static void main(String args[]) {
       
  int result;
       
  try {
           
   System.out.println("in Level 1");
          
    try {
               
    System.out.println("in Level 2");
  // result=100/0;  //Level 2
              
     try {
                  
      System.out.println("in Level 3");
                     
      result=100/0;  //Level 3
               
    }
               
    catch (Exception e) {
                   
     System.out.println("Level 3:" + e.getClass().toString());
               
    }
               
               
    finally {
                   
     System.out.println("In Level 3 finally");
               
    }
               
              
    // result=100/0;  //Level 2
           
    }
           
   catch (Exception e) {
              
     System.out.println("Level 2:" + e.getClass().toString());
          
    }
    finally {
               
    System.out.println("In Level 2 finally");
          
    }
            
   // result = 100 / 0;  //level 1
       
  }
       
  catch (Exception e) {
           
   System.out.println("Level 1:" + e.getClass().toString());
       
  }
       
  finally {
          
.    System.out.println("In Level 1 finally");
       
  }
   
 }
}
result
in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally

 

4, hands-on brain

 

 

Source


public class SystemExitAndFinally {
   
 public static void main(String[] args)
    {
       
  try{
           
   System.out.println("in main");
           
   throw new Exception("Exception is thrown in main");
              //System.exit(0);
       
  }
       
  catch(Exception e)
         {
           
   System.out.println(e.getMessage());
           
   System.exit(0);
       
  }
       
  finally
       
  {
           
   System.out.println("in finally");
       
  }
   
 }

}
result
in main
Exception is thrown in main
When there exists try throw new Exception (), finally will not be executed
 
Class tests
 
Enter an integer to determine grades, throws an exception
 
import java.util.Scanner;
class MyException extends Exception
{
 public MyException(String messege)
 {
  super(messege);
 }
}
class numbertest // custom exception class
{
 public int Score (int A) // throws MyException when a <0 or a> 100, throw a custom exception
 {
  
  IF (a <0 || A> 100) 
  {
   the throw new MyException ( "input error score"); // throws an exception
  }  
  return A; // returns A
 }
}
public class test1{
 public static void main(String[] args) throws MyException
 {
  try
  {
   Scanner scan=new Scanner(System.in);
   System.out.println("请输入分数");
   int n = 0,i;
   n=scan.nextInt();
   numbertest k=new numbertest(); 
   try
   {
    int t=k.score(n);
    i=n/10;
    switch(i)
    {
    case 10:
    case 9:
     System.out.println("优");break;
    case 8:
     System.out.println("良");break;
    case 7:
     System.out.println("中");break;
    case 6:
     System.out.println ( "pass"); BREAK;
    default:
     System.out.println ( "fail"); BREAK;
    }
   }
   the catch (MyException E)
   {
    System.out.println (E); // Output
   }
   }
   the catch (exception E) // int Since the variable is defined as a type, the character input, the output of the abnormality information
   {
    System.out.println ( "invalid input format"); // output
   }
  }
}
result

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/songxinai/p/11755410.html