C#用户定义从Dll获取异常的异常处理获取异常未被用户代码处理 [英] C# User Defined Exception handling for Erroe from Dll get Exception was unhandled by user code

查看:397
本文介绍了C#用户定义从Dll获取异常的异常处理获取异常未被用户代码处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用动态c#应用程序,将所有类都从我单独创建的Dll文件链接到主应用程序,在这些文件中,当我连接动态的dll文件时,错误处理程序希望通过连接抛出错误我曾经是在这里是我尝试做的



我有一个dll文件与这个编码和类上



class clsGlobles
{

  public object dlststus = false; // dtabase文件状态

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

}
else
{
抛出新异常(某些重要文件丢失 - 请重新安装应用程序); //抛出这个错误并停止运行程序

}
}
catch(Exception ex)
{
throw ex; //抛出异常到抛出异常,我喜欢交给
// Evan我使用throw;而不是抛出我得到相同的结果
}
}

然后我链接这个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()); //我在dll里面的错误我应该来这里,我可以从这里处理
}

}



当我关闭上面的错误框和继续运行它也显示这样的一个



解决方案

我想我在这里得到答案,当我通过引用将我的 dll 链接到应用程序中,然后将其作为我的应用程序使用指令,它可以正常工作,并允许我使用应用程序 throw strong>的 throw catch 语句,b当它通过动态添加到应用程序中时,它希望我处理我在 dll 中的异常,并解决有什么问题,它不允许我通过抛出异常使用throw new例外(err)应用程序



Evan没有错误处理 throw new exception 可以,但是不要让catch块中的 throw

i am working with dynamic c# application that links all the classes into the main application from the Dll files that i create separately, in these files when i connect my dll file dynamically the error handlers want throw the errors by the connection as it used to be here is what i try to do

i have a dll file with this coding and class on it

class 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
    }
}

and then i link this dll file by using dynamic method it works well but when i try to pass the user define error then i get error as unhanded exception and being show the class inside the dll file, i don't wants to handle any exception in my classes in dll file just need to pass them to the next step and handle them in the program where i use them.

here is the code where i use it

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
            }

}  

and when i close the above error box and continue run it also shows something like this

解决方案

i think i get the answer here, when i link my dll into the application by reference and then using it as a object inside of my application by using directive it works fine and lets me to use throw exception to the application's throw catch statement, but when it added into the application by dynamically it expect me to handle my exceptions inside the dll and solve what ever the problems, it wont allow me to throw exceptions by using throw new exception("err") to the application

Evan a there is no err handling for that throw new exception is okay, but it wont allow the throw in catch block

这篇关于C#用户定义从Dll获取异常的异常处理获取异常未被用户代码处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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