473,395 Members | 1,456 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,395 software developers and data experts.

Try Catch Opinion

Hello All,

I know you shouldn't issue a 'return' from a finally block, but how about a
catch block?

If while in a method call you fall into a catch and don't want to continue
processing the rest of the code in the method is it ok to 'return' from the
catch?

Thanks,
John
Oct 3 '06 #1
6 2886
John F <jf@rt.comwrote:
Hello All,

I know you shouldn't issue a 'return' from a finally block, but how about a
catch block?

If while in a method call you fall into a catch and don't want to continue
processing the rest of the code in the method is it ok to 'return' from the
catch?
I don't see why not, although I think it is not very clean. Even if you
"return" from a catch block, the code in the finally block will be executed,
and thus, the flow is not obviously clean. In fact, it makes much more sense
to return from a finally block then a catch block for this reason alone [I
would do neither].

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Oct 3 '06 #2
Yes, you can return from catch block it is completely correct.
--
HTH
Stoitcho Goutsev (100)

"John F" <jf@rt.comwrote in message
news:CC**********************************@microsof t.com...
Hello All,

I know you shouldn't issue a 'return' from a finally block, but how about
a
catch block?

If while in a method call you fall into a catch and don't want to continue
processing the rest of the code in the method is it ok to 'return' from
the
catch?

Thanks,
John

Oct 3 '06 #3
Sure, it's ok.

Realize that if you have

try
{ yada yada yada.....}
catch
{ return;}
finally
{... some cleanup code }

The code in the finally block is executed immediately AFTER the return
statement.

I do agree with another poster that this may decrease readability of your
code. If I need some construct like this, I typically comment at the top
and bottom of the method (and probably in the try/catch area also) in a way
that i can't overlook.

"John F" <jf@rt.comwrote in message
news:CC**********************************@microsof t.com...
Hello All,

I know you shouldn't issue a 'return' from a finally block, but how about
a
catch block?

If while in a method call you fall into a catch and don't want to continue
processing the rest of the code in the method is it ok to 'return' from
the
catch?

Thanks,
John

Oct 3 '06 #4
Thanks to all for the input. The particular scenarios I want to implement
won't incorporate a finally block.

Thanks...

"John F" wrote:
Hello All,

I know you shouldn't issue a 'return' from a finally block, but how about a
catch block?

If while in a method call you fall into a catch and don't want to continue
processing the rest of the code in the method is it ok to 'return' from the
catch?

Thanks,
John
Oct 3 '06 #5
Best Practices for Handling Exceptions:
http://msdn.microsoft.com/library/de...Exceptions.asp

chanmm

"John F" <jf@rt.comwrote in message
news:CC**********************************@microsof t.com...
Hello All,

I know you shouldn't issue a 'return' from a finally block, but how about
a
catch block?

If while in a method call you fall into a catch and don't want to continue
processing the rest of the code in the method is it ok to 'return' from
the
catch?

Thanks,
John

Oct 4 '06 #6
Just as an FYI, you should have more finally blocks than catch blocks.

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------
"John F" <jf@rt.comwrote in message
news:56**********************************@microsof t.com...
Thanks to all for the input. The particular scenarios I want to implement
won't incorporate a finally block.

Thanks...

"John F" wrote:
>Hello All,

I know you shouldn't issue a 'return' from a finally block, but how about
a
catch block?

If while in a method call you fall into a catch and don't want to
continue
processing the rest of the code in the method is it ok to 'return' from
the
catch?

Thanks,
John

Oct 4 '06 #7

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

Similar topics

7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
6
by: ChrisB | last post by:
Hello All: I notice that when using try/catch blocks in C#, variables declared in the try block go out of scope in the finally block. So, for example, the following code generates a compiler...
7
by: Arjen | last post by:
Hi, I'm doing this: try { try { } catch(Exception ex){ throw;
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
6
by: Martin Ortiz | last post by:
Which is best approach? Should Try + Catch be used to only deal with "catastrophic" events (like divide by zero, non-existant file, etc...etc...) Or should Try + Catch be used IN PLACE of...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
34
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
4
by: tshad | last post by:
Normally, I surround my Dataset/fill or DBreader execut with a try/Catch. Something like: ****************************************************** Dim dbReader As SqlDataReader Dim...
5
by: Back 9 | last post by:
I try to insert try/catch statement in every single method of my c# code to prevent the app from crashing not nicely. Do you think it is good idea? I remember that an artical says " do not overuse...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
I have to read data from an Excel Sheet. Using Microsoft Office Interop and Automation I create an Excel object Dim xl As New Excel.Application, wkbk As Excel.Workbook, rng as Excel.Range I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.