java中的ArithmeticException

/*
 * Exception中有一个特殊的子类异常RuntimeException运行时异常。
 * 如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。
 * 如果在函数上声明了该异常。调用者可以不用进行处理,编译一样通过。
 * */
class Demo
{
	int div(int a, int b) throws ArithmeticException
	{
		return a/b;
	}
}

class ExceptionDemo {

	public static void main(String[] args)
	{
		Demo d = new Demo();
		int x = d.div(4, 0);
		System.out.println("x="+x);
		System.out.println("Over");
}
}
/*  * Exception中有一个特殊的子类异常RuntimeException运行时异常。  * 如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。  * 如果在函数上声明了该异常。调用者可以不用进行处理,编译一样通过。  * */ class Demo { int div(int a, int b) throws ArithmeticException { return a/b; } } class ExceptionDemo { public static void main(String[] args) { Demo d = new Demo(); int x = d.div(4, 0); System.out.println("x="+x); System.out.println("Over"); } }
经验分享 程序员 微信小程序 职场和发展