apocalypse
apocalypse

Reputation: 5884

Exception in constructor while creating com object

What exception should I throw if creating com object fails (in my own class instance constructor)? For example I want to create Excel.Application object. If it fails, I want to throw specific Exception with inner exception filled with COMException generated by Excel.Application constructor.

Upvotes: 0

Views: 80

Answers (1)

Bill Gregg
Bill Gregg

Reputation: 7147

If you want to create your own class of exceptions you can, but you don't have to do that to wrap a new exception around an inner exception.

public class CustomException : Exception
{
}

public static void main()
{

    try
    {
       //Code that instantiates COM object.

    }
    catch(Exception ex)
    {
       throw new CustomException("This is my message.  I can put anything I want to, then pass the real exception as the inner exception", ex);
    }

}

Upvotes: 1

Related Questions