C# User Defined Exception handling for Erroe from Dll get Exception was unhandled by user code

余生长醉 提交于 2019-12-03 00:51:40

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/38816233/c-sharp-user-defined-exception-handling-for-erroe-from-dll-get-exception-was-unh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!