简体   繁体   English

在try块c#中重新抛出异常

[英]Rethrow an exception in a try block c#

My first question here and I am not that great at english so please bear with me, 我在这里提出的第一个问题是英语不是很好,所以请耐心等待,

I am writing an application that allows users to write scripts that interface with 'drivers', the scripts and drivers are all separate class library dll's. 我正在编写一个应用程序,允许用户编写与“驱动程序”接口的脚本,脚本和驱动程序都是独立的类库dll。 These classes communicate through callback delegates that are passed , so at compile-time they are not linked. 这些类通过传递的回调委托进行通信,因此在编译时它们不会被链接。

example: (Script)-->(The program that handles communication)-->(drivers) 示例:(脚本) - >(处理通信的程序) - >(驱动程序)

Now my question is: 现在我的问题是:

When a script executes a method via a delegate and it throws an exception, the exception is bubbled back up to the script and the user can handle it if they catch it in a try-catch block, if not, the exception has to be caught inside my program. 当脚本通过委托执行方法并抛出异常时,异常会冒泡回到脚本中,如果用户在try-catch块中捕获它,用户可以处理它,如果没有,则必须捕获异常在我的程序里面。

It works fine like this, but I do not know if this is the right way to go: 它工作正常,但我不知道这是否是正确的方法:

delegate object ScriptCallbackDelegate(string InstanceName, string MethodName, object[] Parameters);

static private object ScriptCallbackMethod(string InstanceName, string MethodName, object[] Parameters)
{
    try
    {
         return InterfaceWithDriver(InstanceName, MethodName, Parameters);
    }
    catch( Exception e )
    {
         try
         {
             throw;
         }
         catch
         {
             Console.WriteLine("Script did not handle exception: " + e.Message);
             return null;
         }
    }

}
catch (Exception e)
{
    try
    {
        throw;
    }
    catch
    {
        Console.WriteLine("Script did not handle exception: " + e.Message);
        return null;
    }
}

is semantically identical to: 在语义上与:

catch (Exception e)
{
    Console.WriteLine("Script did not handle exception: " + e.Message);
    return null;
}

The script is never seeing that inner throw - it is being caught by your C# code. 该脚本永远不会看到内部throw - 它被C#代码捕获。

Exception caused in your code never leaves it, eg in following you can see a similar behavior. 代码中导致的异常永远不会离开它,例如在下面你可以看到类似的行为。

using System;

namespace Code.Without.IDE
{
    public static class TryCatch
    {
        public static void Main(string[] args)
        {
            try
            {
                try
                {
                    throw new Exception("Ex01");
                }
                catch(Exception ex)
                {
                    try
                    {
                        throw;
                    }
                    catch
                    {
                        Console.WriteLine("Exeption did not go anywhere");
                    }
                }
                Console.WriteLine("In try block");
            }
            catch
            {
                Console.WriteLine("In catch block");
            }
        }
    }
}

generates following output: 生成以下输出:

------ C:\\abhi\\Code\\CSharp\\without IDE\\TryCatch.exe ------ C:\\ abhi \\ Code \\ CSharp \\没有IDE \\ TryCatch.exe

Exeption did not go anywhere 执行没有去任何地方

In try block 在try块中

------ Process returned 0 ------进程返回0

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM