- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
嵌套的 Try/Catch 是否表示您的编码不干净?我想知道,因为在我的 catch 中,我正在调用另一个方法,如果失败,我会收到另一个运行时错误,所以我很想再次使用另一个 try/catch 将这些调用包装在 catch 中。我想知道这样做是否正常?
例如
catch (Exception ex)
{
transaction.VoidOrder(transactionID);
LogError(ex.ToString());
Response.Redirect("Checkout", false);
}
所以 VoidOrder
甚至 LogError
方法都可能被炸毁。现在,当我调用 VoidOrder
时,我在 transactionID
上得到一个 null ref,因为它调用了一个 BL 方法,并且在该 BL 方法中我重新抛出以便我可以捕获它在上面代码的这个更高级别。但是,如果我再次在 catch 区内 throw ,那么我也需要接住它。
最佳答案
以下是我们解决问题的方法:
从 UI/代码隐藏级别到其他层的所有调用都使用 try-catch,我们总是在其中捕获自定义异常。底层采取的所有操作都有自己的 try-catch,它记录、包装和抛出自定义异常。然后,UI 可以依赖于此并使用友好的错误消息查找已处理的异常。
代码隐藏:
protected void btnSubmit_Click(object sender, EventArgs e)
{
//do something when a button is clicked...
try
{
MyBL.TakeAction()
}
catch(MyApplicationCustomException ex)
{
//display something to the user, etc.
ltlErrorPane.Text = ex.Message;
//or redirect if desired
if(ex.ErrorType == MyCustomErrorsType.Transactional)
{
Response.Redirect("~/Errors/Transaction.aspx");
}
}
}
BL:
在业务层,任何可能失败的操作都使用 try-catch,在将问题抛给 UI 之前记录并包装问题。
public class MyBL
{
public static void TakeAction()
{
try
{
//do something
}
catch(SpecificDotNetException ex)
{
//log, wrap and throw
MyExceptionManagement.LogException(ex)
throw new MyApplicationCustomException(ex, "Some friendly error message", MyCustomErrorsType.Transactional);
}
finally
{
//clean up...
}
}
}
异常处理程序:
实际的异常处理程序有多种记录方式,包括事件日志、文件日志和最后的电子邮件(如果所有其他方法都失败了)。如果记录器不能执行任何预期的操作,我们选择简单地返回 false。 IMO 这是个人选择。我们认为 3 种方法连续失败的可能性(事件日志失败、尝试文件日志、失败、尝试电子邮件、失败)的可能性很小。在这种情况下,我们选择允许应用程序继续运行。您的另一种选择是允许应用程序完全失败。
public static class MyExceptionManagement
{
public static bool LogException(Exception ex)
{
try
{
//try logging to a log source by priority,
//if it fails with all sources, return false as a last resort
//we choose not to let logging issues interfere with user experience
//if logging worked
return true;
}
catch(Exception ex)
{
//in most cases, using try-catch as a true-false is bad practice
//but when logging an exception causes an exception itself, we do
//use this as a well-considered choice.
return false;
}
}
}
最后,作为故障保险,我们实现了 Application_Error
全局事件处理程序(在 Global.asax
中)。对于我们没有正确 try catch 某些东西的情况,这是最后的手段。我们通常记录并重定向到一个友好的错误页面。如果上述自定义错误处理成功完成,则很少有错误会进入全局处理程序。
希望对您有所帮助。这是一种可能的解决方案。多年来,它在一些较大的应用程序上对我们来说效果很好。
关于c# - 嵌套尝试/捕捉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2313696/
如何知道 WM_DEVICECHANGE 的到来? WndProc 被覆盖。我收到了所有消息,但没有一条是 WM_DEVICECHANGE 类型的。 RegisterDeviceNotificatio
我想创建一个滚动的表面列表框,它会在拖动完成后自动捕捉到一个位置,以便屏幕上的中心项目在视口(viewport)中居中。 我已经得到了中心项,但现在像往常一样,WPF 处理大小、屏幕位置和偏移量的方式
如果我有一个使用步长的范围 slider ,例如从 0 到 100,步长为 5,然后我在该范围之间有一个随机值,比如说 56,我如何确定最接近的捕捉值 (55) 是多少? 我正在考虑向前和向后循环,直
我想添加到模块记录器中的所有功能。我想记录函数的“开始”时间和“结束”时间。这样我就可以获得每个函数(同步函数)的执行时间。 但我不知道如何让它工作.. =(我不想以某种动态方式重写函数 - 我想在将
我正在编写一个 C# 应用程序,如果文件已被某个进程使用,我必须在其中显示一条消息,如果该文件不存在,则应用程序需要显示另一条消息。 像这样: try { //Code to open a f
所以我正在使用城市词典 api,他们的术语可以链接到其他使用 [term] 和 api 的框,我想使它们实际上在 markdown 中超链接,即 term所以我尝试制作一个替换正则表达式来做到这一点我
我有一个使用鼠标滚轮 jQuery 插件的水平滚动网站。滚动有效,但我想将每个“文章”捕捉到文档的左侧,这样一旦停止滚动,它就不会停留在一半的位置。 我目前的标记: CSS #viewport { w
我需要在 windows 上录制声音。我需要写信来传输我从演讲者那里听到的内容。我可以依赖什么方法/API? 最佳答案 您可以使用 DirectSound ;可以找到 sample here这是针对麦
for ii = 1:2:2*de.nP G=[one, aux3(:,ii), aux3(:,ii) - aux2(:,ii),aux3(:,ii+1) - a
您好,感谢阅读。我是编程、C# 和套接字编程方面的新手。在我的代码中,我尝试发现问题以在我的应用程序中提供容错能力。以下内容: catch (ArgumentNullException
我有一个函数可以运行用户生成的正则表达式。但是,如果用户输入了一个不会运行的正则表达式,那么它就会停止并跌倒。我试过将行包装在 Try/Catch block 中,但是没有任何反应。 如果有帮助,我正
嵌套的 Try/Catch 是否表示您的编码不干净?我想知道,因为在我的 catch 中,我正在调用另一个方法,如果失败,我会收到另一个运行时错误,所以我很想再次使用另一个 try/catch 将这些
我不知道如何放置一个相对于其同级路径边界框的路径。想象一个像窗口一样的盒子,我想在它的右上角放一个关闭按钮。这是在变换窗口(缩放 3 倍)后组合在一起的框和关闭按钮: 我只是在
我正在尝试使用 UISlider 实现某种形式的捕捉或步骤。我编写了以下代码,但它并没有像我希望的那样顺利。它可以工作,但是当我向上滑动它时,它会向右移动 5 个点,使手指不在“滑动圆”的中心 这是我
目前,我使用 Snap SVG 创建一个小型建模工具。 var p = Snap.path(pathString).attr({fill:'none', stroke:'black', strokeW
我希望能够使用 Snap SVG 将一组集合作为一个组进行拖动。到目前为止,我只能将子集(矩形和文本框)作为一个项目进行拖动,但我想要实现的是集体“表格”是可拖动的,而其中的字段仍保留为单独的形状,因
我有一段代码可以像这样抛出和捕获错误 try { } catch (e: FooException) { } catch (e: BarException) { } finally { } 并且有一些
我定义了一个自定义错误处理程序,它捕获所有异常并将它们保存到日志中。现在,如果我在 mysqli 查询中出现语法错误,例如拼写错误,页面会在此时完全停止加载。不会引发异常,因此不会触发错误处理程序,也
我有以下分组的svg文件,g的id为flower-petals,然后里面的每个部分都是花瓣,我似乎无法更改每个内部路径的填充。 我将 snap 的 petals 变量定义为 petals.Snap("
当脚本执行 Read-Host cmdlet,关闭窗口不会激活 finally堵塞。下面是一个随意但功能最少的示例。我正在使用 PowerShell 5.0。 Beep() 只是为了让 finally
我是一名优秀的程序员,十分优秀!