public boolean A()throws Exception
{
 try
 {
    if(..)
    {
       return true;
    }   
    else
       return false;
 }
 catch(Exception e)
 {
   throw(e);
                   //return true;   (用写这句么?)
 }
}
我想问一下,如果我在catch里面抛出异常,然后是否还要写return true;
大家帮我看看,谢谢大家

解决方案 »

  1.   

    catch 是不应该出现 return 语句的。你上面的代码应该这样写比较合适:
    先定义一个 ret变量
    boolean ret = fasle中间改变这个 ret的值最后返回这个 ret 变量。
      

  2.   

    throw(e);后面的语句是不执行的!
    如果出错时,需要返回的值,则不抛出异常。
      

  3.   

    public boolean A()throws Exception
    {
     try
     {
        if(..)
        {
           return true;
        }   
        else
           return false;
     }
     catch(Exception e)
     {
    //   throw(e);
       System.out.println(e);
      return true; 
     }
    这样不就行了.
    }
      

  4.   


    public boolean A()throws Exception
    {
     try
     {
        if(..)
        {
           return true;
        }   
        else
           return false;
     }
     catch(Exception e)
     {
    //   throw(e);
       System.out.println(e);
      return true; 
     }
    //这样不就行了.
    }应该是这样才行
      

  5.   

    to  shiney2000(水水) 
    呵呵.
      

  6.   

    throw 出new Exception时候,不用return
    正确的做法:
    多个catch 处理能处理的,throw不能处理的try{
    }catch(ExceptionOne e1){
     ....
      retrun false;
    }catch(ExceptionTwo e2){
     ....
      retrun false;
    }catch(Exception e3){
     ....
      throw (e3);
    }
      

  7.   

    假如无论程序是否会产生异常,你都需要返回一个boolean值 
    你完全可以finally语句块中return boolean 
    try
    {
      if( ...)
      {
        ...;
      }
      if( ...)
      {
        ...;
      }
    }
    catch(Exception e )
    {}
    finally
    {
      return true;
    }  
      

  8.   

    那这个方法某种程度上说就没有执行的必要了!因为无论如何都是true
      

  9.   

    做一个bool标志位,在try catch外边return就好了,放到finally里返回也是一样,不过听说不赞成这样做,其实也没有什么大惊小怪
      

  10.   

    throw e以后就不需要任何代码了, 因为这时方法已经退出并返回e的引用了.声明了抛出异常的方法不一定返回指定的类型, 因为可能返回的是异常类.
      

  11.   

    public boolean A()throws Exception中throws Exception可以去掉,与下面的重复。
      

  12.   

    楼上的:
    throws Exception是方法声明, 和下面的throw new Exception()不是一回事!!
    一个是throws, 一个是throw.
      

  13.   

    楼上的意思是public boolean A() throws Exception
    {
        boolean bRet = true;    try
        {
            if(...)  //true
            {
                //...
            }
            else
            {
                bRet = false;
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
            e.printStackTrace();
            bRet = false;
        }    return bRet;
    }中的throws Exception可以去掉。异常或抛出,或捕捉,既然捕捉了,就不要抛出了
      

  14.   

    关于return放在哪里。上面的都已经说的差不多了。 如 yubokings(天涯) 的方法;在这里。我说一点:
    try {
      ...
    }
    catch( Exception e) {
      ...
      throw( new Exception());
      //在你收到异常的时候再抛出异常?
      //编写者期望在函数A外面的try块中接收这个异常。结果并非你所期望的哦!
    }
    测试代码:
    ======================================================================
    class MyException extends Exception {    private String errMesg;
        MyException(String str) {
            errMesg = str;
        }
        public String toString() {
    return errMesg;
        }
    }class MyException extends Exception {    private String errMesg;
        MyException(String str) {
            errMesg = str;
        }
        public String toString() {
            return errMesg;
        }
    }public class CatchTest0 {    static void checkError(int nParam) {
            try {
                System.out.println("checkError("+nParam+");==========================");
                funA(nParam);
                System.out.println("\n");
            } catch(Exception e) {
                System.out.println("函数抛出异常:"+e);
            }
        }    static Boolean funA(int nParam) throws Exception {        //做向上转型;
            try {
                if(nParam==1) {
                    System.out.print("内部 空");
                    throw new NullPointerException("内部:空指针;1");//手动产生异常
                }
                else if(nParam==2) {
                    System.out.print("内部 数");
                    throw new IndexOutOfBoundsException("内部:数组越界;2");
                }
                else if(nParam==3) {
                    System.out.print("内部 运");
                    throw new ArithmeticException("内部:运算异常;3");
                }
                else {
                    System.out.println("我没错");
                    return true;
                }        } catch(ArithmeticException e) {
                System.out.println("---我接收到了 运");
                try {
                    int[] arr = {5,4,3};
                    arr[5] = 1;//自动创建异常
                } catch(ArrayIndexOutOfBoundsException ee) {
                    System.out.println("下标越界;");
                } finally {
                    throw new MyException("外部:我的 运算异常");
                }        } catch(IndexOutOfBoundsException e) {
                System.out.println("---我接收到了 数");
                throw(e);//抛出接收到的异常        } catch(NullPointerException e) {
                System.out.println("---我接收到了 空");
                throw new MyException("外部:我的 空指针");//抛出自定义的异常。//        无效的捕捉。系统未产生MyException类异常!
    //        } catch(MyException e) {
    //            System.out.println("receive a exception.");
    //            throw new MyException("BAD INOF");        //已经获取异常,无须再抛出!
    //
            } finally {
                return true;
                //函数funA() 返回true;
            }
        }    public static void main(String[] args) {        checkError(1);
            checkError(2);
            checkError(3);
            checkError(4);
        }
    }
    结果为:---------------------------------------意料之外吧~~
    D:\MyJava\Catch>javac CatchTest0.javaD:\MyJava\Catch>java  CatchTest0
    checkError(1);==========================
    内部 空---我接收到了 空checkError(2);==========================
    内部 数---我接收到了 数checkError(3);==========================
    内部 运---我接收到了 运
    下标越界;checkError(4);==========================
    我没错D:\MyJava\Catch>
      

  15.   

    如果您想得到所期望的结果,可参考下面的做法:==================================================
    class MyException extends Exception {    private String errMesg;
        MyException(String str) {
            errMesg = str;
        }
        public String toString() {
            return errMesg;
        }
    }public class CatchTest1 {
        static void checkError(int nParam) {
            try {
                System.out.println("checkError("+nParam+");==========================");
                funA(nParam);
                System.out.println("我没错!\n");
            } catch(MyException e) {
                System.out.println("函数抛出异常:"+e);
            }
        }    static Boolean funA(int nParam) throws MyException {        //做向上转型;        if(nParam==3) {
                System.out.println("---我接收到了 运");
                throw new MyException("外部:我的 运算异常");
            }
            else if(nParam==2) {
                System.out.println("---我接收到了 数");
                throw new MyException("外部:我的 数组越界");
            }
            else if(nParam==1) {
                System.out.println("---我接收到了 空");
                throw new MyException("外部:我的 空指针");
            }
            else {
                return true;
                //函数funA() 返回true;
            }
        }    public static void main(String[] args) {        checkError(1);
            checkError(2);
            checkError(3);
            checkError(4);
        }
    }
    --------------------------------------------------
    D:\MyJava\Catch>javac CatchTest1.javaD:\MyJava\Catch>java  CatchTest1
    checkError(1);==========================
    ---我接收到了 空
    函数抛出异常:外部:我的 空指针
    checkError(2);==========================
    ---我接收到了 数
    函数抛出异常:外部:我的 数组越界
    checkError(3);==========================
    ---我接收到了 运
    函数抛出异常:外部:我的 运算异常
    checkError(4);==========================
    我没错!
    D:\MyJava\Catch>