C#:分析“不安全”方法调用 [英] C#: Analyze "unsafe" method invokes

查看:132
本文介绍了C#:分析“不安全”方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VS10开发人员可以使用不安全方法调用(即实际使用ReSharper类似的ReSharper)方法 - 即调用这个调用,调用堆栈在任何一个安全块(try-catch)中无法实现?

  class A 
{
public static vois f()
{
try
{
...
B.DoSome(); //安全通话,处理的异常
}
catch(异常e)
{
...
}
}

public static void f2()
{
... //没有try-catch块
B.DoSome(); //可能的未处理的异常
}
}

class B
{
public static void DoSome()
{
。 ..
//没有try-catch块,可能引发未处理的异常
}
}


解决方案

假设您希望确保您的应用程序不会因未处理的异常而崩溃,可以通过订阅 UnhandledException 事件

注意:请 不要在每个方法中放置try-catch 如你的例子所示。


How (actual with which instuments like ReSharper) in VS10 developer can find "unsafe" method invokes - i.e. invokes, which unattainable by call stack in no one safe block (try-catch) ?

class A
{
    public static vois f()
    {
        try
        {
            ... 
            B.DoSome(); // safe call, exceptions handled
        }
        catch(Exception e)
        {
            ...
        }
    }

    public static void f2()
    {
        ... //no try-catch block
        B.DoSome(); // possible unhandled exception
    }
}

class B
{
    public static void DoSome()
    {
        ...
        //no try-catch block, possible to raise unhandled exception
    }
}

解决方案

Assuming you would like to make sure your application is not crashing due to an unhandled exception, this can be easily done by subscribing to the UnhandledException event of the AppDomain.

Note: Please don't put a try-catch in every method as your sample suggests.

这篇关于C#:分析“不安全”方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆