151227ExceptionDemo

来源:互联网 发布:荼靡 知乎 编辑:程序博客网 时间:2024/06/08 15:04
package java151227;
//RuntimeException


/*
 * Exception中RuntimeException比较特殊!
 * 如果在函数内抛出该异常:函数上可以不用声明,编译通过
 * 调用者可以不用处理,编译一样通过!
 */
public class ExceptionDemo {
public static void main(String[] args) {
Demo demo =new Demo();
int x= demo.div(4, 0);
System.out.println("x="+x);
System.out.println("over!");

}


}


//ArithmeticException  
class Demo{
/**
* 两个整型数相除
* @param a 被除数
* @param b 除数
* @return 返回商
*/
int div(int  a ,int  b) throws  ArithmeticException
{
// if (b==0) {
// throw new ArithmeticException("除数为0!");
// }
return a/b;
}
}

0 0
原创粉丝点击