catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

解决方案 »

  1.   

    catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }
      

  2.   

    catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;'or //exit Function         }
      

  3.   

    好好看看try /catch/finally结构吧.
     如果一定要返回的话,在finally里返回就行了,不管怎么样,finally总是会执行的.
      

  4.   

    public static int Add(int firstParam, int secondParam)
         {
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    return (firstParam + secondParam);
                }
                else
                {
                    throw new Exception("Input Error");
                }            
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
           return 0;
        }
      

  5.   

    使用契约式的设计思想,你必须和调用者定义好这个函数的错误处理方式,有两种选择方式:
    1,出现问题,抛出异常.那么你根本不需要使用try,catch,直接让调用者来作这件事情.
        public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            if ((firstParam >= 0) && (secondParam >= 0) )
            {
                count = firstParam + secondParam;
            }
            else
            {
                throw new Exception("Input Error");
            }            
                
           return count;
        }2.你也可以用特定的返回值来表示输入错误,比如返回-1表示参数错误.
    你可以写成
        public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            if ((firstParam >= 0) && (secondParam >= 0) )
            {
                count = firstParam + secondParam;
            }
            else
            {
                count = -1;
            }            
                
           return count;
        }
    你自己抛出异常,有自己catch的做法是没有什么意义的,只会降低性能.
      

  6.   

    to:
    catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }return后程序仍可往下执行,引发异常后仍会执行
    如何才可以:
    在引发异常后不会返回count
      

  7.   

    catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;   //-1失败
            }
      

  8.   

    兄弟,看了半天,终于发现你的错误所在。
    你的意思说是如果相加成功,则返回值,否则抛出异常,对吧,
    这样的话,就不要加TRY-CATCH了,呵,把TRY--CATCH去掉,就是你要的效果
      

  9.   

    所以应该这样写:
    public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;
                }
                else
                {
                    throw new Exception("Input Error");
                }            
                return count;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
           return count;
      

  10.   

    最好的办法:
        public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;                
                }
                else
                {
                    throw new Exception("Input Error");
                }            
                return count;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    楼上的解决方法都受楼主的影响了。
      

  11.   

    try
    {
    }
    catch
    {
    }
    finally
    {
        return count;
    }
      

  12.   

    TO:thinhunan(仁渣) 
      你这样写会提示函数没有return 的语法错误
      

  13.   

    TO: turnmissile(会翻跟头的导弹)
    以下是我在网上找的throw ,try ...catch在一起用的情况,楼上说的没有看清我的题目
    http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/exceptions/throw/throw.src
      

  14.   

    turnmissile(会翻跟头的导弹)的意见才是对的.编程的算法才是最重要的,你不要拘泥于你现在的代码,你不想返回值,那么你的调用一定有异常处理才行,所以返回你的自定义Exception吧!
      

  15.   

    你看错了吧?http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/exceptions/throw/throw.src
    处有try ...catch,但没有自己catch自己抛出的Exception呀?
      

  16.   

    catch{//err;  return -1;}你既然有return  就必须预防return 的错误要不就放弃try catch结构throw new Exception;
      

  17.   

    public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;                
                }
                else
                {
                    throw new Exception("Input Error");
                }            
                return count;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);        }
        }
      

  18.   

    public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;                
                }
                else
                {
                    throw new Exception("Input Error");
                }            
                return count;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }
      

  19.   

    如果你确认你要处理这个异常,并且返回一个-1之类的信息来代表错误,那你就catch里面return -1如果你希望这个异常交给调用者去处理
    直接throw 一个异常就可以了
      

  20.   

    public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            int isPass = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;
                }
                else
                {
                    throw new Exception("Input Error");
                } 
               
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
           return count;
        }用throw来抛出异常吧,再由你的上层程序来捕捉处理....
      

  21.   

    public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            int isPass = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;
                }
                else
                {
                    throw new Exception("Input Error");
                } 
                 isPass = 1
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }       return isPass;
        }用throw来抛出异常吧,再由你的上层程序来捕捉处理....
      

  22.   

    要出现异常不返回,最简单的方法就是不用try ... catch
    非要try的地方不捕获异常就是了,直接try ... finally
    捕获异常交给调用的地方来做
      

  23.   

    代码写得简单一点
    public static int Add(int firstParam, int secondParam)
     {
    int count = 0;
    if ((firstParam >= 0) && (secondParam >= 0) )
    {
    count = firstParam + secondParam;
                            return count;
    }
    throw new Exception("Input Error");

    }
      

  24.   

    public static int Add(int firstParam, int secondParam)
    {}
    ---------------------------
    你的方法是要求有返回值的,而且是整形(int)的,所以我认为执行此方法定有return intValue;(包括此方法中的异常)!
    本人认为:既然是返回整形的方法就让他全部的返回整形,在if( ....) { ;} else{ count = -1;}
    return count; ,在应用方法的时候进行判断,如果是该方法返回的是 -1,则Console.WriteLine("Input Error");个人观点,仅供参考!
      

  25.   

    public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;
                }
                else
                {
                    throw new Exception("Input Error");
                }            
                
            }
            catch (Exception e)
            {
                /* Console.WriteLine(e.Message); //ERROR */
                Console.WriteLine(e.Message);
                Return;
            }
           return count;
        }
      

  26.   

    to kinghuhua:
    我觉得你可能错误理解了异常处理的机制.
    如果你throw一个异常出来,他不会继续执行下面的代码,而是直接跳到后面的catch代码中去.所以如果你不catch这个错误,他就不会返回count值,而是抛出了一个异常.这是一种可行的替代方法.从语法原则上面,是不可能出现一个函数有的时候返回int数据,有时返回null的.你所期望的是不可能实现的任务.
      

  27.   

    同上,个人认为楼主该多试试就能体会到到底什么是TryCatch,什么是Throw
      

  28.   

    如果你一定要用
     public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;
                }
                else
                {
                    throw new Exception("Input Error");
                    count = -1;
                }            
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
           return count;
        }
      

  29.   

    ==========================================================================================
    public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            int isPass = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;
                }
                else
                {
                    throw new Exception("Input Error");
                } 
                 isPass = 1
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }       return isPass;
        }用throw来抛出异常吧,再由你的上层程序来捕捉处理....========================================================================================
      

  30.   

    public static int Add(int firstParam, int secondParam)
         {
            int count = 0;
            int isPass = 0;
            try
            {
                if ((firstParam >= 0) && (secondParam >= 0) )
                {
                    count = firstParam + secondParam;
                }
                else
                {
                    throw new Exception("Input Error");
                } 
                 isPass = 1
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return 0;  //主要是这里,随便返回一个特定的值,反正你不用的就可以了。
             }       return isPass;
        }