473,396 Members | 1,966 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,396 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 998
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{
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: 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...

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.