try catch blocks

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
 
J

Jon Skeet

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.
 
M

Mike P

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
 
E

Eliyahu Goldin

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
 
S

Sander Pilon

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.
 
J

james

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top