【发布时间】:2011-04-27 13:36:33
【问题描述】:
我在这里Should a retrieval method return 'null' or throw an exception when it can't produce the return value? 和Should functions return null or an empty object? 发现了问题,但我认为我的情况完全不同。
我正在编写一个由 Web 服务和客户端组成的应用程序。 webservice负责访问数据,并将数据返回给客户端。我这样设计我的应用程序:
//网络服务
try
{
DataTable data = GetSomeData(parameter);
return data
}
catch (OopsException ex)
{
//write some log here
return null;
}
//客户:
DataTable data = CallGetSomeData(parameter);
if(data == null)
{
MessageBox.Show("Oops Exception!");
return;
}
嗯,有一个不返回 null 的规则。我认为我不应该只是重新抛出异常并让客户端捕获 SoapException。你有什么意见?有没有更好的方法来解决这个问题?
谢谢。
【问题讨论】:
-
例外情况允许您tighten your post-conditions。它们还与语言、风格和编码约定密切相关;但你没有指定任何一个。
-
这看起来像是 asp.net,我想?你应该相应地标记它。
标签: exception exception-handling return-value