C# 在另一个函数中重写 try-catch

发布于 2024-11-17 14:35:07 字数 256 浏览 3 评论 0原文

我正在使用从互联网上获得的 dll,用于在 C# 中使用网络摄像头。如果找不到连接的网络摄像头,我想显示类似“无法找到要使用的摄像头。请确认此时没有其他应用程序正在使用您的摄像头,然后重试”。我遇到的问题是 dll 的创建者在他们的 dll 编程中包含了一个 try-catch...所以我的 try-catch 从未看到异常,因为出现了“对象引用未设置为对象的实例”错误(通过 try/catch 格式化为 MessageBox)。有没有办法可以在显示消息并显示我自己的消息之前覆盖内置错误处理?

I am using a dll that I got from the internet for utilizing a webcam in C#. If it can't find a webcam connected I would like to display something like "Unable to find a camera to use. Please verify that no other applications are using your camera at this time and try again". The problem I am having is the creator of the dll included a try-catch in their dll programming...so my try-catch never see's the exception because a "object referenced not set to an instance of an object" error comes up instead (formatted by a try/catch into a MessageBox). Is there a way I can override the built-in error handling before it displays the message and display my own?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

才能让你更想念 2024-11-24 14:35:07

如果您收到“对象引用未设置为对象实例”(NullReferenceException),那么很可能他们实际上没有捕获异常。

如果您想捕获确切的情况(并让您不知道且无法处理的其他错误落空 - 进行异常处理的正确方法),您可以尝试深入了解包含在例外:

class Program
{
    public static void DoSomething()
    {
        string blah = null;
        Console.WriteLine(blah.Length);
    }

    static void Main(string[] args)
    {
        try
        {
            DoSomething();
        }
        catch (NullReferenceException e)
        {
            string methodName = e.TargetSite.Name;
            Console.WriteLine(methodName);

            System.Diagnostics.StackTrace trace =
                new System.Diagnostics.StackTrace(e, true);

            int lineNumber = trace.GetFrame(0).GetFileLineNumber();
            Console.WriteLine(lineNumber);

            if(methodName == "DoSomething" && lineNumber == 13)
            {
                ShowErrorToUser(); // Todo: Implement this
            }
            else
            {
                throw; // Just re-throw the error if you don't know where it came from
            }
        }
    }
}

编辑

在评论中发现它确实被捕获,并显示在消息框中。

我会留下这个答案,因为它适用于类似的情况,但不适用于这种情况。请参阅 OscarMK 的答案。

If you're getting "object referenced not set to an instance of an object" (NullReferenceException), then it is likely they didn't actually catch the exception.

If you want to catch that exact case (and let others errors you don't know about and can't handle fall through - the proper way to do exception handling), you can try getting down and dirty with the stack information included with the exception:

class Program
{
    public static void DoSomething()
    {
        string blah = null;
        Console.WriteLine(blah.Length);
    }

    static void Main(string[] args)
    {
        try
        {
            DoSomething();
        }
        catch (NullReferenceException e)
        {
            string methodName = e.TargetSite.Name;
            Console.WriteLine(methodName);

            System.Diagnostics.StackTrace trace =
                new System.Diagnostics.StackTrace(e, true);

            int lineNumber = trace.GetFrame(0).GetFileLineNumber();
            Console.WriteLine(lineNumber);

            if(methodName == "DoSomething" && lineNumber == 13)
            {
                ShowErrorToUser(); // Todo: Implement this
            }
            else
            {
                throw; // Just re-throw the error if you don't know where it came from
            }
        }
    }
}

Edit

Found out in the comments that it really is being caught, and displayed in a message box.

I will leave this answer since it is applicable to a similar situation, but not applicable to this situation. See OscarMK's answer instead.

作死小能手 2024-11-24 14:35:07

您不能仅重写 try-catch 块,但您可以重写该方法,并基本上执行先前方法正在执行的所有操作并删除 try-catch 块。

You cannot override the try-catch block only, but you could override the method and basically do everything the previous method is doing and removing the try-catch block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文