2023/10/19 动手动脑

首先,一个程序如果碰到了异常不处理,程序就会立即停止,而异常处理就是在异常发生的情况下启动类似于备用方案使程序继续运行

Java中的异常捕获结构由try,catch,finally三部分构成,其中,try和catch是必须同时存在的。

try中的代码就是可能存在异常的代码,catch中的代码就是try中有异常时的执行方案,finally中的代码无论有没有异常都会正常运行

public class Take {
    public static void main(String[] args)
    {
        try {
            String str="hahahaha";
            System.out.println(str+"个锤子");
            int num=Integer.parseInt("23H");//将数字的字符型转换为整数型
            System.out.println(num);
        }catch(Exception e)//如果出现异常就替换异常语句然后执行,一般都填入处理异常的代码
        {
            e.printStackTrace();//输出异常性质
            System.out.println("这里显示异常错误");
        }
        finally//无论是否异常,里面的内容都会执行 
        {
            System.out.println("666");
        }
    }
}

Java中存在自定义异常,当运行时的代码不符合你所自定义的条件时就会抛出该异常,在方法中的异常抛出如果不想在当前方法中解决,就必须抛出该异常,一个方法运行时只能抛出一个异常,当该方法抛出异常后,该方法就不会再往下执行,要让方法抛出异常就要用到throws和throw关键字,前者是在方法的定义后主体前,throws指出了该方法所能抛出的异常,throw是在方法主体内来运用的。

class ClassException extends Exception//自定义异常
{
    public ClassException(String hhh)
    {
        super(hhh);
    }
}
public class students {
    public static void main(String[]args)
    {
        try
        {
            qiandao("张三","未到");
        }
        catch(ClassException a)//处理指定类型的异常
        {
            a.printStackTrace();
            System.out.println(a);
        }
    }
    static void qiandao(String name,String state)throws ClassException//抛出的异常类型,抛出的异常在主方法中处理
    {
        if(state.equals("未到") )
        {
            throw new ClassException(name+"旷课");
        }
    }
}

能被try-catch捕捉的异常都是运行时异常,Java的类库中有很多常见的异常类

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

结果:

ArrayIndexOutOfBoundsException/内层try-catch

发生ArithmeticException

可以看到内层的catch是优先执行的然后再执行外层catch

抛出的异常如果没有被捕捉到就会一直向外传递直到被捕获到

而对于try部分的代码时优先执行外层再执行里层

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

}

Java中的finally按道理时无论有没有异常都会执行的,但是

package hei;


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


}

改代码并没有执行finally

尽管finally块通常会被执行,但有一些特殊情况下它可能不会被执行。下面是这些情况的示例:

在执行try代码块时,JVM退出了。

这可能发生在程序执行期间由于某些原因导致JVM异常退出,如出现硬件故障或操作系统错误。在这种情况下,程序无法继续执行,因此finally块不会被执行。

在执行try代码块时,程序遇到了System.exit()语句。

System.exit()是一个Java源代码级的方法,用于终止当前正在运行的Java虚拟机。调用System.exit()将导致JVM立即退出,而不会执行任何未执行的代码,包括finally块。

在执行try代码块时,发生了无限循环或死锁。

如果在try代码块中发生无限循环或死锁,程序将无法继续执行,因此finally块将不会被执行

posted @ 2023-10-19 22:00  伐木工熊大  阅读(18)  评论(0)    收藏  举报