Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
| Search | Today's Posts | Mark Forums Read
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 15th, 2009, 01:26 AM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default Is it a good coding pratice? Question on Error Handling.

Public Class DALException
Inherits ApplicationException

Const PERMIT_LOGGING As Boolean = False

Public Enum DALExceptionCode
Concurrency
ConnectionFailure
DatabaseFailure
LoginFailure
Constraint
Transaction
Locks
General
End Enum

Private m_ExceptionCode As DALExceptionCode = DALExceptionCode.General

Private m_DataSet As DataSet
Private m_ErrorRows As DataRow()

Private m_Log As New StringBuilder()


Public Sub New(ByVal msg As String, ByVal exc As Exception, ByVal ds As DataSet)
'Set the default message and inner exception
MyBase.New(msg, exc)

m_DataSet = ds

'determine what type of exception we have
If TypeOf exc Is ConstraintException Then 'thrown by the DataSet
m_Log.Append("ConstraintException occured" & vbCrLf)
m_ExceptionCode = DALExceptionCode.Constraint

ElseIf TypeOf exc Is DBConcurrencyException Then
m_Log.Append("DBConcurrencyException occured" & vbCrLf)
m_ExceptionCode = DALExceptionCode.Concurrency

ElseIf TypeOf exc Is SqlClient.SqlException Then
m_Log.Append("SqlClient.SqlException occured" & vbCrLf)
processSQLException(exc)

ElseIf TypeOf exc Is DataException Then
'this should handle all other providers - OLEDB, ODBC
m_Log.Append("DataException occured" & vbCrLf)
m_ExceptionCode = DALExceptionCode.Constraint

ElseIf TypeOf exc Is Exception Then
'anything left
m_Log.Append("Exception occured" & vbCrLf)
m_ExceptionCode = DALExceptionCode.General

End If

detectRowErrors(ds)

m_Log.Append("Original Exception: " & vbCrLf)
m_Log.Append(exc.ToString)
logException(m_Log.ToString)

End Sub


================================================== ===

My questions
==============================================
1. What I can find from the above code is that it has centralized all the Exception Types under a User Defined Exception.
I know it is centralized in a certain manner and maintenance of codes would be easier.
Is it a good practice?


Your helpful advices would certainly shine much light of this matter.
Thanks.
 
Old January 15th, 2009, 02:07 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You really did not need to post the message THREE times.

But then you posted it in the wrong forum. Look at the title of this forum: C#

Your code is VB.NET

Not clear to me that there is any huge advantage in storing the exception code as an enumeration integer when you could just store the *TYPE* of the exception, directly.

And I would personally be paranoid and *NOT* do
ElseIf TypeOf exc Is Exception Then

Instead, I'd just do
Else


 
Old January 15th, 2009, 02:43 AM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks Old Pedant.
Sorry, infact, it was not my intention to post 3 times. My browser prompted me many times that my post was not effected. When it was finally done then, it appeared 3 times.
I tried to delete those extra posts but no delete button found. I'm new to the forum.

Thanks, you are right. But I'll be more specific.. Is it good practice to group all predefined Exceptions [class UserDefinedException as in below example] under a User defined exceptions class and each time when I catch some errors I just throw that User Defined Exception.

int count = 0;

try
{
count = count + 1;
}

catch (Exception ex)
{
throw (new UserDefinedException(ex));
}
 
Old January 15th, 2009, 06:24 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

My personal opinion is that it depends on the *consumers* of your class/code.

If they are used to and expecting conventional exceptions, they will now have to rewrite all their code.

If you are your own consumer, then the question is simple: Does this help YOU write your other code more simply and/or cleaner and/or more maintainably???

But I'm not sure I'm the best person to ask this question of. I'd be interested to see what people such as Dr. Purdum have to say.
The Following User Says Thank You to Old Pedant For This Useful Post:
 
Old January 17th, 2009, 09:30 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hello there..

I was reading this some days ago. There is a silent rule that says "If you don't know which exception to catch, then don't catch anything". why? because if your program has a problem and you just say "there was an error", you will have a lot of trouble trying to find where. There are sometimes when is better to have an error pop up that nothing, or a catch that don't help.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
The Following 2 Users Say Thank You to gbianchi For This Useful Post:
amlesh (January 19th, 2009)
 
Old January 18th, 2009, 07:15 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Good advice! Also "If you can't catch the exception and *FIX* the problem, then maybe you aren't catching it in the right place." There are, of course, some exceptions that can't be fixed, but for those that can be it's important to catch them at the right spot in the code/structure.
The Following 2 Users Say Thank You to Old Pedant For This Useful Post:
amlesh (January 19th, 2009)




Similar Threads
Thread Thread Starter Forum Replies Last Post
Good XSLT coding Architecture for transformation anil2020 XSLT 0 August 27th, 2007 12:10 PM
Coding question aagway Access VBA 1 May 4th, 2004 02:54 PM
Error Handling Question Lucy Classic ASP Basics 5 November 18th, 2003 06:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.