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;
}
}

解决方案 »

  1.   

    你一旦throw一个异常  就跳进了异常处理了 这样就没法return值了
      

  2.   

    如果你改成throws Exception(), 外围函数抛出的是ArithmeticException  所以就不匹配了  肯定会编译报错的
      

  3.   

    public class LL { 
    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;

    }
      

  4.   

    书上说:如果一个方法用throws声明异常, 则调用该方法的函数就必须得处理该异常,否则编译时 会报错!但为什么我编写的如下函数 编译时却不报错呢?
    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;
    }
    }
      

  5.   

    Java codepublic class LL { 
        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;
        } 
    }
    -------------------------------------------------------------------------
      我知道正确的应该这么写,但请问 为什么我这么写却不报错呢?
      

  6.   

    因为异常分为两类:
        1、RuntimeException及其子类,该类异常在语法上不强制必须处理。
        2、除了1以外的其它所有异常,该类一场在语法上要求必须处理。楼主遇到的异常属于第一类,即使使用throws声明也不强制处理。