GROOVY 异常处理 try catch

try catch finally

class Demo{

    static void main(String[] args){
        try{
            File file = new File("./Example.txt");
            FileReader fr = new FileReader(file);
        }catch(Exception ex){
            println ex;
        }finally{
            println "无论程序是否出错,此处的内容都会执行"
        }
    }
}

多异常捕获:

class Demo{

    static void main(String[] args){
        try{
            def arr = new int[3];
            arr[5] = 5;
        }catch(ArrayIndexOutOfBoundsException ex){
            println "Catching the Array out of Bounds exception"
        }catch(Exception ex){
            println "Catching the exception";
        }finally{
            println "无论程序是否出错,此处的内容都会执行"
        }
    }
}

 

posted @ 2025-07-07 16:21  山村放羊娃  阅读(26)  评论(0)    收藏  举报