c# - 用户代码未处理Dll get Exception的C#用户定义的异常处理错误

标签 c# dll error-handling unhandled-exception

我正在使用动态C#应用程序,该程序将所有类从我分别创建的Dll文件链接到主应用程序中,在这些文件中,当我动态连接dll文件时,错误处理程序希望通过连接抛出错误,因为它以前是这是我尝试做的

我有一个具有此编码和类的dll文件

clsGlobles类
{

public object dlststus = false; // dtabase file status

public clsGlobles()
{
    try
    {
        if (dlststus == true)
        {

        }
        else
        {
            throw new Exception("Some important files are missing - Please re-install the application"); //throw this as a error and stop running the program

        }
    }
    catch (Exception ex)
    {
        throw ex; //throw exception to the upper throw catch exception where i like to hand it
    //Evan i use throw; instead of throw ex; i get the same result
    }
}

然后我通过使用动态方法链接此dll文件,它工作得很好,但是当我尝试传递用户定义错误时,我将错误作为未处理的异常并在dll文件中显示该类,我不想处理任何异常在dll文件中的类中,只需将它们传递到下一步,并在我使用它们的程序中处理它们即可。

这是我使用它的代码
private void frmMain_Load(object sender, EventArgs e)
{
    string tf = "";

            tf = Application.StartupPath + "\\clsGlobles.dll";
            try
            {
                Assembly asm = Assembly.LoadFrom(tf);
                AppDomain.CurrentDomain.Load(asm.GetName());
                Type type = asm.GetTypes().First(t => t.Name == "clsGlobles");
                glbls = Activator.CreateInstance(type);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString()); // the error i throw in inside the dll class should come here and i could handle it from here
            }

}  

enter image description here

当我关闭上面的错误框并继续运行时,它还会显示类似这样的内容

enter image description here

最佳答案

我想我在这里得到了答案,当我通过引用将 dll 链接到应用程序,然后通过using指令将其用作应用程序内的对象时,它可以正常工作,并且让我可以将throw异常用于应用程序throw catch语句,但是当它动态地添加到应用程序中时,它希望我在 dll 内部处理我的异常并解决所有问题,它不允许我通过using throw new exception("err")将异常引发到应用程序

Evan a对该throw new exception来说没有错误处理,但是不允许catch块中的throw

关于c# - 用户代码未处理Dll get Exception的C#用户定义的异常处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38816233/

相关文章:

c# - IQueryable<T> 转换为 IList<SpecificInterface>

javascript - ASP.NET MVC jQuery HTML5 数据属性

c++ - 在 C++ 中连接两个 char* 的函数

.net - Msvcr71.dll Msvcp71.dll 丢失

c++ - 从 C++ DLL 打开并写入控制台

从 try、tryCatch 或类似返回逻辑 TRUE/FALSE?

c# - 在 ASP.Net Core 应用程序启动期间运行异步代码

c# - C#中基于string和bool过滤对象列表

javascript - 我可以在 JavaScript 中捕获此 Firestore 消息吗?

c++ - 如何在 for 循环中进行错误捕获