异常(exception):发生在程序执行期间,表明出现了一个非法运行的情况。许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象。例如:数组越界和被0除。

代码验证:

package test;

import javax.swing.*;

class AboutException {
   public static void main(String[] a) 
   {
      int i=1, j=0, k;
    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");
     }
        
  }
}

输出结果:

 

当java程序中出现多try catch的情况时,一定要注意程序执行的先后顺序。

多try catch的java异常处理代码一

package test;

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

程序运行结果:

多try catch的java异常处理代码二

package test;

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

程序运行结果:

当有多个try catch finally嵌套 时,要特别注意finally的执行时机。特别注意:当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句的执行顺序。再者,我们一般认为finally语句中的句子一定会被执行,这里的一定是相对而言的,并不是绝对的。例如以下程序代码的运行:

package test;

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


}

运行结果:

在这段java代码中finally语句块并没有执行。

 

通过过异常的学习,自己尝试了自定义了一个异常类来处理异常,源码如下:

package classtest;

import java.util.Scanner;

class Myexception extends Exception {
    public Myexception(String message) {
        super(message);
    }
}

public class Mytest {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        try{
            function();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("main()函数运行时出现异常");
        }
        finally {
            System.out.println("main()函数运行完毕");
        }
    }

    public static int judge(String str) {
        int c = 0;
        String regex = "[0-9]+";
        String regex1="[-][0-9]+";
        boolean d = str.matches(regex);
        boolean e = str.matches(regex1);
        if (d == false) {
            c = 1;
            if (e == true) {
                c = 0;
            }
        }
        return c;
    }

    public static void function() {
        try {
            System.out.println("请输入一个正整数:");
            Scanner input = new Scanner(System.in);
            String a = input.next();
            int temp = judge(a);
            if (temp == 1) {
                throw new Myexception("不能输入非法字符");
            } else {
                int num = Integer.parseInt(a);
                if (num < 0)
                    throw new Myexception("输入不能为负数");
                else if (num == 0) {
                    throw new Myexception("正整数不包括0");
                }
            }

        } catch (Myexception f) {
            System.out.println(f.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("function()函数运行时出现异常");
        }
       finally {
           System.out.println("function()函数已运行完毕,请指示下一步动作");
       }
        
    }
}

 

 

版权声明:本文为weixiao1717原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/weixiao1717/p/11780111.html