473,545 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error handling issue: which one is better, multiple catches/throw or cast System.Exceptio n to its runtime type?

Exception may be thrown in the code inside the try block.
I want to handling the SqlException with State == 1 in a special way,
and for all others I want to use a general way to handle. Which of the
following options is better?
-----------------------------------------------------
Option 1:
try{...}
catch (System.Data.Sq lClient.SqlExce ption ex)
{
if (ex.State == 1)
{
// special error handling
}
else
throw ex;
}
catch (Exception ex)
{
// general error handling
}
--------------------------------------------------
Option 2:

try{...}
catch (Exception e)
{
if (ex.GetType() == System.Data.Sql Client.SqlExcep tion
&& ((System.Data.S qlClient.SqlExc eption)ex).Stat e ==
1)
{
// special error handling
}
else
{
// general error handling
}

}

Jun 14 '07 #1
2 2186
Carol wrote:
Exception may be thrown in the code inside the try block.
I want to handling the SqlException with State == 1 in a special way,
and for all others I want to use a general way to handle. Which of the
following options is better?
-----------------------------------------------------
Option 1:
try{...}
catch (System.Data.Sq lClient.SqlExce ption ex)
{
if (ex.State == 1)
{
// special error handling
}
else
throw ex;
}
catch (Exception ex)
{
// general error handling
}
If I read you correctly, this won't give you the desired result. A
throw from within a catch block will not cause the exception to be
caught again by another catch in the same try/catch. Instead the
exception will only be caught if there is a higher level try/catch than
what you show above, or if you wrap the code within the first catch in
its own try/catch.

What you might want to consider is placing your "general error handling"
in a separate method that call from either the 'else' that is in your
first catch, or from your second catch.
--------------------------------------------------
Option 2:

try{...}
catch (Exception e)
{
if (ex.GetType() == System.Data.Sql Client.SqlExcep tion
&& ((System.Data.S qlClient.SqlExc eption)ex).Stat e ==
1)
{
// special error handling
}
else
{
// general error handling
}

}
I would do this slightly different (mostly a preference thing), but the
above will basically give you what you want.
--
Tom Porterfield
Jun 14 '07 #2
Thank you, you are absolutely right:)

On Jun 14, 10:40 am, Tom Porterfield <tppor...@mvps. orgwrote:
Carol wrote:
Exception may be thrown in the code inside the try block.
I want to handling the SqlException with State == 1 in a special way,
and for all others I want to use a general way to handle. Which of the
following options is better?
-----------------------------------------------------
Option 1:
try{...}
catch (System.Data.Sq lClient.SqlExce ption ex)
{
if (ex.State == 1)
{
// special error handling
}
else
throw ex;
}
catch (Exception ex)
{
// general error handling
}

If I read you correctly, this won't give you the desired result. A
throw from within a catch block will not cause the exception to be
caught again by another catch in the same try/catch. Instead the
exception will only be caught if there is a higher level try/catch than
what you show above, or if you wrap the code within the first catch in
its own try/catch.

What you might want to consider is placing your "general error handling"
in a separate method that call from either the 'else' that is in your
first catch, or from your second catch.


--------------------------------------------------
Option 2:
try{...}
catch (Exception e)
{
if (ex.GetType() == System.Data.Sql Client.SqlExcep tion
&& ((System.Data.S qlClient.SqlExc eption)ex).Stat e ==
1)
{
// special error handling
}
else
{
// general error handling
}
}

I would do this slightly different (mostly a preference thing), but the
above will basically give you what you want.
--
Tom Porterfield- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Jun 15 '07 #3

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

Similar topics

9
2613
by: Mark Twombley | last post by:
Hi, I'm just getting back into C++ and had a question about the best practice for assigning error numbers. I have been working in VB for sometime now and there you would start assigning error number at vbObjectError + count. Is there a similar practice in C++ or is it just coder preference. Thanks -- Mark Twombley
17
7592
by: ahaupt | last post by:
Hi all, I'm currently writing a load of class libraries, but not the main application iteslf. I want to provide some method for reporting errors back to the main application. At the moment I have a bool errBl and a string errMsg in my classes. When errBl is ture, one could see what the error was in errMsg.
16
2898
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In: http://www.mywebsite.org/WebResource.axd?d=5WvLfhnJp5Lc8WhQSD4gdA2&t=632614619884218750 -------------------------------------------------------------------------------- System.Security.Cryptography.CryptographicException:...
4
1819
by: Paul Wilson | last post by:
I want to use Err.Raise() method to raise my own exceptions. Is this the right way of raising my own exceptions ? (i think this is the only way). What is the Error number i can safely use, ensuring that it isn't used by ..NET. Ie, can i use -100 as a error number?? Ie, -100, -101,-102... etc. I want to konw if .NET uses negative values as...
8
1780
by: jcrouse | last post by:
I am using the following code to trap errors in a sub routine: Try Executable code Catch ex As Exception Dim strInputE As String = Application.StartupPath & "\Error.txt" Dim srE As StreamWriter = File.AppendText(strInputE) srE.WriteLine(vbCr) srE.WriteLine(vbCr) srE.WriteLine(DateTime.Now)
4
1828
by: sm | last post by:
Hi, I have a couple of questions with regards to handling errors and exceptions. 1. If I use On Error goto Errhandler ... Errhandler:
9
2217
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that I have been doing it right. I think I overdo it. Please have a look: -- using System; using System.IO;
132
5470
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid hard-to-find bugs. http://distributed-software.blogspot.com/2007/01/c-exception-handling-is-defective.html Regards, zorabi@ZHMicro.com...
0
7487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.