473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

try catch blocks

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
5 5586
Mike P <mr*@telcoelectronics.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
> 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
Nov 15 '05 #4

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:Or****************@TK2MSFTNGP12.phx.gbl...
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.
Nov 15 '05 #5
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
"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:Or**************@TK2MSFTNGP12.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
69
by: Nay Myo Aung | last post by:
Hi All! I think I discovered a possible improvement on .NET Framwork's TRY...CATCH...END TRY statement. I guess most developers might also have been discovered that at some point in their .NET...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
6
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.