异常Exception

时间:2021-04-13 12:29:17   收藏:0   阅读:0

异常体系结构

java把异常当做对象,基类java.lang.Throwable作为所有异常的超类。

Java API定义了许多异常类,分为两大类,错误Error和异常Exception。

Exception分支重要的一个子类RuntimeException(运行时异常)

抛出和捕获异常

异常处理五个关键字:try监控区域

catch捕获异常

finally善后工作(可以不要,假设IO,资源,关闭。)

  //假设要捕获多个异常,最大的类写在最下面。


        try //try 监控区域
        {
            System.out.println(a/b);
        }catch(Exception e) //catch(想要捕获的类型) 捕获异常
        {
            System.out.println("程序出现异常,b不能为0");
        }catch(Throwable t)
        {

        }
        finally //处理善后工作.
        {
            System.out.println("finally");
        }

throw主要抛出异常,一般在方法中使用.

throws,方法中处理不了,在方法上抛出异常。

 public void test (int a,int b)throws ArithmeticException
    {
        if(b==0)
        {
            throw new ArithmeticException();
        }
    }
}

**Ctrl+Alt+T 包裹代码的快捷键!! **

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!