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
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);
}
}
7 Comments
apocalypse
ANy idea how to name it? InstanceInitializationException or something?
Bill Gregg
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.
apocalypse
COnstructorFailException? Please help me, and suggest name.
Bill Gregg
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.
Bill Gregg
COMInitializationException
|