Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

errors and reflection: what should "try-catch" look like

Status
Not open for further replies.

misterstick

Programmer
Apr 7, 2000
633
GB
i use reflection in my code to run forms, getting the class names to create at runtime from data held in a database.

this works pretty well, if not instantly, but my problem comes when the invoked form throws an error.

i'm using the following code:

Code:
string className = valueFromData;
Form form = this.GetType().Assembly.CreateInstance(className) as Form;
if ( form != null )
{
 // do stuff with it
}

i can put in catches for the errors thrown by CreateInstance itself, but how to catch "Target of Invocation" errors?

FxCop doesn't like "catch ( Exception ex ) { ... }", should i just use "catch { }" and accept that the form pointer will be null?

what do you think?

mtia,

mr s. <;)

 
it's lucky i'm gorgeous, because i'd never get anywhere on brains alone.

TargetInvocationException hs the original error as its InnerException. i'm now using this:

Code:
try
{
 this.GetType().Assembly.CreateInstance(className) as Form;
}
catch ( TargetInvocationException tiex )
{
 throw tiex.InnerException;
}

is this wrong?



mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top