09异常处理

import javax.swing.*;

class AboutException {
   public static void main(String[] a) 
   {
      double 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  "+k);
             //JOptionPane.showInternalConfirmDialog(null, k);
     }
  }


}

Try{
//可能发生运行错误的代码;
}
catch(异常类型 异常对象引用){
//用于处理异常的代码
}
finally{
//用于“善后” 的代码
}

Java 中所有可捕获的异常都派生自 Exception 类。

使用Java异常处理机制

1、Java 中所有可捕获的异常都派生自 Exception 类。

2、把可能会发生错误的代码放进try语句块中。

3、当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。 catch语句块中的代码用于处理错误。

4、当异常发生时,程序控制流程由try语句块跳转到catch语句块。

5、不管是否有异常发生,finally语句块中的语句始终保证被执行。

6、如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

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"); 
        } 
    } 
}

实验结果截图、

动手动脑4:当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。
请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。
特别注意:
当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

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) {//exception异常
                    
                    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");
        
        }
    
    }

}

实验结果截图、

当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

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");
        }    
    }
}

try语句没有被执行到,如在try语句之前return就返回了,这样finally语句就不会执行。这也说明了finally语句被执行的必要而非充分条件是:相应的try语句一定被执行到。

在try块|catch块中有System.exit(0);这样的语句。System.exit(0)是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。

在try-catch-finally中, 当return遇到finally,return对finally无效

编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。



import java.util.*;
public class ttt {
public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
String pro;
System.out.println("请输入成绩:");
while(true){
try{
pro=sc.nextLine();
if(pro.matches("\D*")){
throw new InException("输入有误!不是的不是数字!");
}
else{
int proo=Integer.parseInt(pro);
System.out.print("输入正确!");
judge(proo);
break;
}
}
catch(InException e){
System.out.print("请重新输入:");
}
}
sc.close();
}

public static void judge(int pro){
if(pro>=0&&pro<60)
System.out.println("成绩不合格!");
else if(pro<70)
System.out.println("成绩合格!");
else if(pro<80)
System.out.println("成绩中!");
else if(pro<90)
System.out.println("成绩良!");
else if(pro<=100)
System.out.println("成绩优!");
else
System.out.println("输入成绩不符");
}
}


class InException extends Exception{//
public InException(String msg){
super(msg);
}
}

 

原文地址:https://www.cnblogs.com/xhxdeblog/p/6102662.html