try catch blocks

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I'm trying to split up a large procedure into several try catch blocks
in order to catch different errors. However, when I split my code up
like this, my variables that I am using throughout the procedure are now
only recoginsed within their own try catch block instead of the whole
procedure.

How do you get around this? By making all your variables public?


Any help would be much appreciated.

Cheers,

Mike
 
Mike P said:
I'm trying to split up a large procedure into several try catch blocks
in order to catch different errors. However, when I split my code up
like this, my variables that I am using throughout the procedure are now
only recoginsed within their own try catch block instead of the whole
procedure.

How do you get around this? By making all your variables public?

Declare the variables outside the try block, instead of inside.
 
I realised I should declare my variables outside the block about 5
seconds after I posted...sorry!

However, if I want to put a try catch block within a switch statement do
I do this exactly the same way as normal? Or do I put a return before
the break to exit the procedure if I catch an error?


Mike
 
However, if I want to put a try catch block within a switch statement do
I do this exactly the same way as normal? yes

Or do I put a return before
the break to exit the procedure if I catch an error?
This question is not specific to switch statement. Everything depends on
your program logic and what you want to do on errors.

Eliyahu
 
Mike P said:
I realised I should declare my variables outside the block about 5
seconds after I posted...sorry!

However, if I want to put a try catch block within a switch statement do
I do this exactly the same way as normal? Or do I put a return before
the break to exit the procedure if I catch an error?


Mike

You can also just throw different exceptions in a single try-catch block,
and figure out the different errors from the type of exception.
 
I think it is important to note that try catch blocks
are no different than any other block as far as scoping
of variables go. Any time you use { } you have created
a new scope. Variables declared inside of { } will not be seen
outside that scope.

JIM
 
Back
Top