-2

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
            }

}  

enter image description here

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

enter image description here

Aylian Craspa
  • 422
  • 5
  • 11
  • 2
    I have trouble understanding your question. Do you mean to use `throw;` instead of `throw ex;` to avoid 'resetting' the stacktrace? – Jeroen Vannevel Aug 07 '16 at 16:31
  • im trying to throw the exception in the variable ex to the next level – Aylian Craspa Aug 07 '16 at 16:47
  • i can do this in a normal class that linked to my project in run time but when it comes to this (loading it by DLL ) it wont work as i expected – Aylian Craspa Aug 07 '16 at 16:52
  • The MessageBox.Show() call was quite unhelpful to show the reason for the exception, TargetInvocationException.Message never tells you anything you need to know. So you clicked the Thrown checkbox in the Exceptions dialog. That worked. Both for the original exception *and* the very unnecessary `throw ex;` statement. Basic ways to get ahead is to improve your MessageBox reporting, ex.Message is quite insufficient, you need ex.ToString() to know enough, and to delete the pointless try/catch/throw code. And untick the Thrown checkbox. – Hans Passant Aug 07 '16 at 17:43
  • that's not my problem bro i just wants to know how to pass that throw new exception without getting exception unhandle error, in my class inside the dll and to he form where i wuld like to handl that error – Aylian Craspa Aug 07 '16 at 17:52

1 Answers1

0

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

Aylian Craspa
  • 422
  • 5
  • 11