Nick
Nick

Reputation: 533

C# Custom Exception non-invocable or type not valid

I have a separate project in my solution, a library (seclib.dll), for handling custom error messages. This is the whole contents

using System;
using System.IO;

namespace seclib
{
    public class Exception : System.Exception
    {
        [Serializable]
        public class FileNotFound : IOException
        {            
            public FileNotFound() { }
            public FileNotFound(string message)
                : base(message) { }
            public FileNotFound(string message, Exception inner)
                : base(message, inner) { }
        }

        public class ProcessException : ApplicationException
        {
            public ProcessException() { }
            public ProcessException(string message)
                : base(message) { }
            public ProcessException(string message, Exception inner)
                : base(message, inner) { }
        }
    }
}

When I try to use it from my main project in the solution (reference exists), like

        try
        {
            //some code here:
            //1. try open a non existing file -> File not found
            //2. I read the file but is empty, or I dont recognise contents -> Process exception manually
        }
        catch (System.IO.FileNotFoundException e)
        {
            throw seclib.Exception.FileNotFound("file error message", e);
        }
        catch (System.ApplicationException e)
        {
            throw seclib.Exception.ProcessException("some other error message", e);
        }

I have the error (before compile) "class seclib.Exception.FileNotFound" (or, ProcessException in the second catch) and "Non-invocable member 'Exception.FileNotFound cannot be used as a method' (same for next catch, i.e. Exception.ProcessException.. etc). If I delete the parenthesis i.e. used it as throw seclib.Exception.FileNotFound; etc., I receive 'Exception.FileNotFound' is a type, which is not valid in the given context.

Could someone please help me? Thank you very much

Upvotes: 1

Views: 194

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

You need to create a new instance of the exception you want to throw:

// notice the "new" keyword
throw new seclib.Exception.FileNotFound("file error message", e);

Is Exception right?

The constuctors defined for FileNotFound and ProcessException currently take an inner Exception as an argument, but Exception in that scope refers to the enclosing seclib.Exception class - change the constructor signatures to:

public FileNotFound(string message, System.Exception inner)  // System.Exception will resolve to the type you expect
            : base(message, inner) { }

Are your catch expressions right?

Since FileNotFound and ProcessException inherit from the base types you're currently catching, you might end up nesting either multiple times - maybe you'll want to test for that:

try {

}
catch (System.IO.FileNotFoundException e)
{
    if(e is seclib.Exception.FileNotFound)
        // Already wrapped in our custom exception type, re-throw as is
        throw;

    throw new seclib.Exception.FileNotFound("file error message", e);
}

or

try {

}
catch (seclib.Exception)
{
    // already wrapped, move along
    throw;
}
catch (System.IO.FileNotFoundException e)
{
    throw new seclib.Exception.FileNotFound("file error message", e);
}
catch (System.ApplicationException e)
...

Upvotes: 1

Related Questions