Nested Try-Catch ?

Is it possible to nest try-catch ?

What if I do something like that :

Try 'Do some stuff Try 'do stuff Catch e As IOException system.debuglog("Error accessing file.") End Try Catch e as NilObjectException 'Do stuff end try

Would it be a valid structure ?

Yes.

But you can also chain the Catch clauses for different exceptions.

[quote=471575:@Michel Bujardet]Is it possible to nest try-catch ?

What if I do something like that :

Try 'Do some stuff Try 'do stuff Catch e As IOException system.debuglog("Error accessing file.") End Try Catch e as NilObjectException 'Do stuff end try

Would it be a valid structure ?[/quote]

This way you only catch ioexception in the inner try catch. You have different options as @Kem Tekinay suggests. You can also do a catch all at the bottom of a method.

Personally I’d suggest keeping your try catch block as small and close to the origination of the error as possible
And only catch the most restrictive of exceptions (ie/ do NOT catch RuntimeException !)
Catch what you can actually handle and no more

I’ve seen some 1000+ line methods with one giant CATCH at the bottom and trying to report anything useful from that error handler is a mess

Like this ?

Try 'Do some stuff Catch e As IOException Log("There was an IOException") Catch e as NilObjectException Log("There was a NilObjectException") end try

Yes.

Damn, Kem was faster :wink:

But i can offer this: http://documentation.xojo.com/api/code_execution/catch.html :smiley:

Though in your particular example, I’d catch RuntimeException, raise End and ThreadEnd, and use Introspection to discover the type of the others.

yes
just be careful if you have some that are subclasses of others that you list the MOST SPECIFIC subclasses first :slight_smile:

ie/ if you did

     catch re as RuntimeException
           // some code

    catch iox as IOException
         // some code

the first catch would eat ALL exceptions and the rest would be useless :slight_smile:

I’d respectfully disagree
State specifically the things you really can handle and leave the rest alone

I miss On error resume next :wink:

Na, don’t :wink: