开发者社区> 问答> 正文

如何处理编译时异常?

已解决

如何处理编译时异常?

展开
收起
文艺cyt 2022-04-03 22:26:34 830 0
1 条回答
写回答
取消 提交回答
  • 推荐回答

    对于编译异常,要么使用try...catch处理掉,要么继续抛出。例1:当调用带有throws关键字的方法时,使用try...catch对异常进行捕获处理。

    package cn.itcast.demo03;

    public class Test { public static void main(String[] args) { int[] arr = null; int i; try{ i = fun(arr); System.out.println(i); }catch(Exception e){ e.printStackTrace(); System.out.println("aaaaa"); } System.out.println("bbbbb"); }

    //自定义的带有throws的方法,抛出的时非RuntimeException的异常。
    public static int fun(int[] arr) throws Exception{
        if(arr == null){
            throw new Exception("数组不存在");
        }
        return arr[3];
    }
    

    }

    2022-04-03 23:13:16
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
JS 语言在引擎级别的执行过程 立即下载
《0代码搭应用》 立即下载
不止代码 立即下载