472,988 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Pointless Exception Handling?

I just came across this code. The exception handling seems completely
pointless. Am I missing something?

try
{
con.Open();
da.Update(dsdata, "Products");
con.Close();
}
catch (SqlException)
{
throw;
}

Is there actually *any* good reason to catch an exception and then
immediately rethrow it without doing anything else?

Jul 9 '08 #1
5 990
Nope, that code is completely useless. If there was a transaction being done
with the update and the rollback was being done within the catch block I
could see it being of use then. As the code is written right now, I'd just
get rid of it.

"Cramer" <A@B.comwrote in message
news:e8**************@TK2MSFTNGP02.phx.gbl...
>I just came across this code. The exception handling seems completely
pointless. Am I missing something?

try
{
con.Open();
da.Update(dsdata, "Products");
con.Close();
}
catch (SqlException)
{
throw;
}

Is there actually *any* good reason to catch an exception and then
immediately rethrow it without doing anything else?

Jul 9 '08 #2
Is there actually *any* good reason to catch an exception and then
immediately rethrow it without doing anything else?
The only useful thing that may be ascertained from this is that you have a
developer that needs talking to :-)

Pete

Jul 9 '08 #3
Cramer wrote:
I just came across this code. The exception handling seems completely
pointless. Am I missing something?

try
{
con.Open();
da.Update(dsdata, "Products");
con.Close();
}
catch (SqlException)
{
throw;
}

Is there actually *any* good reason to catch an exception and then
immediately rethrow it without doing anything else?
I can think of only one: the developer could think of no other way to
quickly see if this particular code was throwing an exception, so they added
an empty handler to set a breakpoint in. It saves you having to decode a
call stack that may have been mangled by intermediate handlers, I suppose.

A better way of diagnosing such problems is to tell the debugger to break
whenever an exception is thrown (as opposed to when it's handled). This will
also allow you to track down when exceptions are being thrown needlessly
(even if they're handled afterwards).

--
J.
Jul 9 '08 #4
On Jul 9, 8:35*am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
Is there actually *any* good reason to catch an exception and then
immediately rethrow it without doing anything else?

The only useful thing that may be ascertained from this is that you have a
developer that needs talking to :-)
There is *one* temporary use for it - it makes it easy to add a
breakpoint for when the exception is thrown *just* at that location. I
wouldn't be surprised to learn that the code had been added during a
debugging session, and then the developer forgot to remove it.

Jon
Jul 9 '08 #5
JPK
Yes ! I do this all the time. NOte, if the code is in constant
development, and a "work in progress" then this is not a problem for me. If
the code is packages and sold to consumers then that is a different story.

JIM
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:ce**********************************@34g2000h sf.googlegroups.com...
On Jul 9, 8:35 am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
Is there actually *any* good reason to catch an exception and then
immediately rethrow it without doing anything else?

The only useful thing that may be ascertained from this is that you have a
developer that needs talking to :-)
There is *one* temporary use for it - it makes it easy to add a
breakpoint for when the exception is thrown *just* at that location. I
wouldn't be surprised to learn that the code had been added during a
debugging session, and then the developer forgot to remove it.

Jon

Jul 9 '08 #6

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

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
1
by: jsnX | last post by:
hi, i would like to make some code that operates on sets of points - does convex hulls and other operations. but it looks like i will need a lot of pointless object copies: ...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.