加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JAVA 异常

发布时间:2020-12-15 07:16:42 所属栏目:Java 来源:网络整理
导读:使用try…catch public class Test { public Test() { // TODO Auto-generated constructor stub } static void main(String[] args) { TODO Auto-generated method stub try { System.out.println( "try start" ); int i = 1 / 0 ; System.out.println( "tr

使用try…catch

public class Test {

    public Test() {
        // TODO Auto-generated constructor stub
    }

    static void main(String[] args) {
         TODO Auto-generated method stub
        try {
            System.out.println("try start");
            int i = 1 / 0;
            System.out.println("try end");
        } catch (Exception e) {
             TODO: handle exception
            System.out.println("catch");
            e.printStackTrace();
        } finally {
            System.out.println("finally");
        }
    }

}

运行结果

start catch java.lang.ArithmeticException: / by zero at Test.main(Test.java:12)

运行时异常

TODO Auto-generated method stub if (true) { RuntimeException e = new RuntimeException("产生异常"throw e; } } }

运行结果

Exception in thread "main" java.lang.RuntimeException: 产生异常
    at Test.main(Test.java:12)

使用throws

void main(String[] args) throws Exception { ) { Exception e = new Exception("产生异常" java.lang.Exception: 产生异常 at Test.main(Test.java:12)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读