to kinghuhua: 我觉得你可能错误理解了异常处理的机制. 如果你throw一个异常出来,他不会继续执行下面的代码,而是直接跳到后面的catch代码中去.所以如果你不catch这个错误,他就不会返回count值,而是抛出了一个异常.这是一种可行的替代方法.从语法原则上面,是不可能出现一个函数有的时候返回int数据,有时返回null的.你所期望的是不可能实现的任务.
同上,个人认为楼主该多试试就能体会到到底什么是TryCatch,什么是Throw
如果你一定要用 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; }
{
Console.WriteLine(e.Message);
return;
}
{
Console.WriteLine(e.Message);
return;'or //exit Function }
如果一定要返回的话,在finally里返回就行了,不管怎么样,finally总是会执行的.
{
try
{
if ((firstParam >= 0) && (secondParam >= 0) )
{
return (firstParam + secondParam);
}
else
{
throw new Exception("Input Error");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return 0;
}
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的做法是没有什么意义的,只会降低性能.
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}return后程序仍可往下执行,引发异常后仍会执行
如何才可以:
在引发异常后不会返回count
{
Console.WriteLine(e.Message);
return -1; //-1失败
}
你的意思说是如果相加成功,则返回值,否则抛出异常,对吧,
这样的话,就不要加TRY-CATCH了,呵,把TRY--CATCH去掉,就是你要的效果
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;
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);
}
}
楼上的解决方法都受楼主的影响了。
{
}
catch
{
}
finally
{
return count;
}
你这样写会提示函数没有return 的语法错误
以下是我在网上找的throw ,try ...catch在一起用的情况,楼上说的没有看清我的题目
http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/exceptions/throw/throw.src
处有try ...catch,但没有自己catch自己抛出的Exception呀?
{
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); }
}
{
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;
}
}
直接throw 一个异常就可以了
{
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来抛出异常吧,再由你的上层程序来捕捉处理....
{
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来抛出异常吧,再由你的上层程序来捕捉处理....
非要try的地方不捕获异常就是了,直接try ... finally
捕获异常交给调用的地方来做
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");
}
{}
---------------------------
你的方法是要求有返回值的,而且是整形(int)的,所以我认为执行此方法定有return intValue;(包括此方法中的异常)!
本人认为:既然是返回整形的方法就让他全部的返回整形,在if( ....) { ;} else{ count = -1;}
return count; ,在应用方法的时候进行判断,如果是该方法返回的是 -1,则Console.WriteLine("Input Error");个人观点,仅供参考!
{
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;
}
我觉得你可能错误理解了异常处理的机制.
如果你throw一个异常出来,他不会继续执行下面的代码,而是直接跳到后面的catch代码中去.所以如果你不catch这个错误,他就不会返回count值,而是抛出了一个异常.这是一种可行的替代方法.从语法原则上面,是不可能出现一个函数有的时候返回int数据,有时返回null的.你所期望的是不可能实现的任务.
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;
}
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来抛出异常吧,再由你的上层程序来捕捉处理....========================================================================================
{
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;
}