10.7抛出异常处理
生活随笔
收集整理的这篇文章主要介绍了
10.7抛出异常处理
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
异常的处理方式:------抛出异常处理
抛出异常处理(throw throws)
注意事项:1.如果一个方法的内部抛出一个异常 对象,那么必须要在方法上声明抛出2.如果调用了一个声明抛出异常的方法 ,那么调用者必须要处理异常。3.如果一个方法内部抛出了一个异常对象,那么throw语句后面的代码都不会在执行。throw和throws 两个关键字的区别:1.throw关键字是用于方法内部的,throws是用于方法声明上的。2.throw关键字是用于方法内部抛出一个异常对象的,throws关键字是用于在方法声明上抛出异常类型。3.throw关键字后面只能有一个异常对象,throws后面一次可以声明抛出多种类型的异常。
class ThrowException {public static void main(String[] args) throws Exception //抛给JVM{try{div(3,0);}catch (Exception e){System.out.println(" 出现异常 ");e.printStackTrace();}}public static void div(int a,int b)throws Exception {if(b == 0){throw new Exception();}int c = a/b;System.out.println(" c= "+c);} }
class ThrowException {public static void main(String[] args) throws Exception //抛给JVM{try{div(3,0);}catch (Exception e){System.out.println(" 出现异常 ");e.printStackTrace();}}public static void div(int a,int b)throws Exception {if(b == 0){throw new Exception();}int c = a/b;System.out.println(" c= "+c);} }
总结
以上是生活随笔为你收集整理的10.7抛出异常处理的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 10.6 捕获处理异常
- 下一篇: 11.1自定义异常类