0

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.

1 Answer 1

1

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);
    }

}
Sign up to request clarification or add additional context in comments.

7 Comments

ANy idea how to name it? InstanceInitializationException or something?
That's entirely up to you. No one else will guess what you named it. If they want to trap your excpetion specifically they will know of it's existence and know what you named it.
COnstructorFailException? Please help me, and suggest name.
It's just a name. Name it. You wouldn't ask me to name your baby. Don't ask me to name your code for you.
COMInitializationException
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.