3

i'm writing a dll which is a wrapper to a access database. and i'm pretty new to c# in general as my background is in web development LAMP with perl, i'm not sure what's a good way to return error to a calling app in case they pass the wrong parameters to my functions or what not.

I have no idea as of now except to probably do some msgbox or throw some exceptions but i don't know where to start looking. Any help or resources would be more than useful :)

thanks~

Brian
  • 117,631
  • 17
  • 236
  • 300
melaos
  • 8,386
  • 4
  • 56
  • 93

6 Answers6

12

You probably don't want to display message dialogs from within your dll, that's the job of the client application, as part of the presentation layer.

.Net library assemblies typically bubble up exceptions to the host application, so that's the approach I'd look at.

public static class LibraryClass
{
    public static void DoSomething(int positiveInteger)
    {
        if (positiveInteger < 0)
        {
            throw new ArgumentException("Expected a positive number", "positiveInteger");
        }
    }
}

Then it's up to your host application to handle those exceptions, logging and displaying them as appropriate.

try
{
    LibraryClass.DoSomething(-3);
}
catch(ArgumentException argExc)
{
    MessageBox.Show("An Error occurred: " + argExc.ToString());
}
Scott Ferguson
  • 7,690
  • 7
  • 41
  • 64
  • 4
    I hate dialog boxes tied to libraries. – ojblass May 05 '09 at 03:48
  • if i add the dll file by dynamically using reflection this error want get throw and it gives me an error as exception unhandled by user , any ideas here my quetion http://stackoverflow.com/questions/38816233/c-sharp-user-defined-exception-handling-for-erroe-from-dll-get-exception-was-unh?noredirect=1#comment65000685_38816233 – Aylian Craspa Aug 07 '16 at 18:19
3

Wrong parameters are usually handled by throwing a ArgumentException or one of its subclasses.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
2

You want to throw an exception.

See

http://msdn.microsoft.com/en-us/library/ms229007.aspx

for the most common framework exceptions, such as ArgumentException and InvalidOperationException. See also

http://msdn.microsoft.com/en-us/library/ms229030.aspx

Brian
  • 117,631
  • 17
  • 236
  • 300
2

Check out Design Guidelines for Class Library Developers: Error Raising and Handling Guidelines

Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
  • That link is for 2003, it is a little out-of-date in that it advocates for (rather than against) ApplicationException, but the rest of the advice still seems timely. – Brian May 05 '09 at 03:51
1

Dlls generally should not create any kind of UI element to report an error. You can Throw (same meaning as raise) many different kinds of exceptions, or create your own and the calling code (client) can catch and report to the user.

public void MyDLLFunction()
{
    try
    {
        //some interesting code that may
        //cause an error here
    }
    catch (Exception ex)
    {
        // do some logging, handle the error etc.
        // if you can't handle the error then throw to
        // the calling code
        throw;
        //not throw ex; - that resets the call stack
    }
}
Gary.Ray
  • 6,441
  • 1
  • 27
  • 42
0

throw new exception?

Paulo
  • 7,123
  • 10
  • 37
  • 34