public class LL
{
public static void main(String[] args)
{
new Test().divide(6, 0);
}
}class Test
{
public int divide(int x, int y) throws ArithmeticException, ArrayStoreException //为什么我编译没错误,不是说throws的异常,
//调用他的方法必须得处理吗? 还有,奇怪的是如果改为throws Exception 一编译就立马出错,请诸位高手指教 先谢谢啦
{
//throw new ArithmeticException(); return x / y;
}
}
{
public static void main(String[] args)
{
new Test().divide(6, 0);
}
}class Test
{
public int divide(int x, int y) throws ArithmeticException, ArrayStoreException //为什么我编译没错误,不是说throws的异常,
//调用他的方法必须得处理吗? 还有,奇怪的是如果改为throws Exception 一编译就立马出错,请诸位高手指教 先谢谢啦
{
//throw new ArithmeticException(); return x / y;
}
}
public static void main(String[] args) {
System.out.print(new Testt().divide(6, 0));
}
} class Testt {
int i;
public int divide(int x, int y){
try{
i = x/y;
}
catch(ArithmeticException aExcep){
System.err.print("Divided by 0");
}
return i;
}
}
public class LL
{
public static void main(String[] args)
{
new Test().divide(6, 0);
}
}class Test
{
public int divide(int x, int y) throws ArithmeticException, ArrayStoreException
{
return x / y;
}
}
public static void main(String[] args) {
System.out.print(new Testt().divide(6, 0));
}
} class Testt {
int i;
public int divide(int x, int y){
try{
i = x/y;
}
catch(ArithmeticException aExcep){
System.err.print("Divided by 0");
}
return i;
}
}
-------------------------------------------------------------------------
我知道正确的应该这么写,但请问 为什么我这么写却不报错呢?
1、RuntimeException及其子类,该类异常在语法上不强制必须处理。
2、除了1以外的其它所有异常,该类一场在语法上要求必须处理。楼主遇到的异常属于第一类,即使使用throws声明也不强制处理。