简体   繁体   English

有没有办法在任何未捕获的异常期间执行代码块?

[英]Is there a way to execute a block of code during any uncaught exception?

I realize this is a shot in the dark and not the best practice or way of going about it, but I want to do specific logging information as the exception is thrown. 我意识到这是一个黑暗的镜头,而不是最佳实践或解决方法,但是我想做特定的日志记录信息,因为会抛出异常。 It is important that the state of the web form does not change. Web表单的状态不要更改,这一点很重要。

You can use 

    try
    {

    }
    catch(Exception ex)
    {
      //your treatment does not contain throw ex.
      //mute exception in order to not change state
    }

You can use this in your webform or in your gloabl.asax in Application_Error (in order to centralize exception treatment)
try
{
    ExceptionProneCode()
}
catch(Exception ex)
{
    WriteToSomeLogFile(ex.Message);

    //if you want the exception to bubble up from this point, 
    //than you would type "throw;"
}

如果我们在ASP.NET上下文中进行交谈,则Application_Error事件将是放置全局异常日志记录块的好地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在catch块中捕获异常后,是否可以再次在try块中执行代码? - Is it possible to execute the code in the try block again after an exception in caught in catch block? 在VS2008中使用Curly Braces {}包围代码块的任何方法? - Any way to surround code block with Curly Braces {} in VS2008? 有什么办法可以先重定向页面然后执行剩余的代码 - Is there any way to redirect the page first then execute the remaining code 如果在 C# 中处理该异常期间引发另一个异常,是否有任何方法可以捕获原始异常? - is there any way to catch original exception if another exception raise during handeling that in C#? 有什么办法可以延迟-针对IQueryable执行委托 <T> 执行后/执行中? - Is there any way to delay- execute a delegate against an IQueryable<T> after/during execution? 在WaitForSeconds期间协程执行代码 - Coroutine execute code during WaitForSeconds 如果有未处理的异常,finally 会阻塞执行吗? - Does finally block execute if there is an unhandled exception? 这是“处理”异常的更好方法,我想让气泡冒泡但执行清理代码 - Which is the better way to “handle” an exception I want to let bubble up but execute clean up code 代码块之前和之后的执行方法 - Execute Method Before and After Code Block 为什么 finally 块中的代码不执行? - Why code in finally block doesn't execute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM