• 动手动脑-异常处理


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

    try catch finally捕捉错误可以让我们快速的找到我们错误的原因

    try来捕捉错误通过catch中的e参数来报出错误的原因可以在finally预先设置好出错之后也会输出的句式。

    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捕捉到抛出的那个异常,然后匹配到的异常处理,然后又抛出了一个异常,这个异常匹配到了异常处理,所以最下面的那个没有被执行。

    输出结果是

    ArrayIndexOutOfBoundsException/内层try-catch
    发生ArithmeticException

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

    这个异常捕捉是:先抛出异常然后异常不匹配,下面又有一个异常想抛出,但是这个异常还没有被处理所以这个新的异常不会被抛出,第一个异常与下啊一个也不匹配,直到最后那个可以与它匹配的异常处理才结束了第一个抛出的异常

    所以输出结果是:

    ArrayIndexOutOfBoundsException/外层try-catch

    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=100/0的位置来测试,我们可以看到他的异常处理是根据他所在的“层”来优先进行匹配

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

    这个代码的结果我们可以看出System.exit(0)可以结束进程不运行finally的内容

    // UsingExceptions.java
    // Demonstrating the getMessage and printStackTrace
    // methods inherited into all exception classes.
    public class PrintExceptionStack {
       public static void main( String args[] )
       {
          try {
             method1();
          }
          catch ( Exception e ) {
             System.err.println( e.getMessage() + "
    " );
             e.printStackTrace();
          }
       }
    
       public static void method1() throws Exception
       {
          method2();
       }
    
       public static void method2() throws Exception
       {
          method3();
       }
    
       public static void method3() throws Exception
       {
          throw new Exception( "Exception thrown in method3" );
       }
    }

    控制台输出:

    Exception thrown in method3
    java.lang.Exception: Exception thrown in method3
     at PrintExceptionStack.method3(PrintExceptionStack.java:28)
     at PrintExceptionStack.method2(PrintExceptionStack.java:23)
     at PrintExceptionStack.method1(PrintExceptionStack.java:18)
     at PrintExceptionStack.main(PrintExceptionStack.java:8)
    从这个结果我们可以看出他显示输出了抛出异常的那个信息然后通过 e.printStackTrace();输出了异常通过的路径
    import java.util.Scanner;
    
    class MyException extends Exception
    {
        public MyException(String Message) {
            super(Message);
        }
        public MyException(String message, Throwable cause) {
            super(message, cause);
        }
         public MyException( Throwable cause) {
            super(cause);
        }
    
    }
    public class text {
         public static void throwExceptionMethod() throws MyException
           {
              
              try {
                 //System.out.println( "Method throwException" );
    
                  throw new Exception("输入错误");
              }
              catch( Exception e )
              {
                 //System.err.println(
                  //  "Exception handled in method throwException" );
                 //转换为一个自定义异常,再抛出
                  throw new MyException("输入错误");
                 
    
                 
              }
              finally
              {
                  
              }
    
              // any code here would not be reached
           }
         public static void main(String[] args) {
            Scanner shuru=new Scanner(System.in);
            int a;
            System.out.println("请输入");
            try
            {
                a=shuru.nextInt();
            }
            catch(Exception e)
            {
                try
                {
                    throwExceptionMethod();
                }
                catch(MyException e1)
                {
                    System.err.println(e1.getMessage());
                }
            }
        }
    
    }

    这个是自己定义的一个关于输入异常的捕捉,如果输入的不是int类型他就会报错

  • 相关阅读:
    用php爬取网页
    无论我是一只菜鸟笨鸟
    有线网卡与无线网卡同时使用
    scapy 命令理解
    Wireshark Filter
    python OS/pdb 模块及数据类型基础
    scapy down and install
    python 字符操作函数
    python 类型集
    python 科学计算
  • 原文地址:https://www.cnblogs.com/yizhixiaozhu/p/11782221.html
Copyright © 2020-2023  润新知