Click here to Skip to main content
15,895,667 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Crystal report KeycodeV2.dll Pin
Dave Kreskowiak4-Feb-06 7:07
mveDave Kreskowiak4-Feb-06 7:07 
QuestionFinding Least & Max value Pin
Chandan_Kr3-Feb-06 0:11
Chandan_Kr3-Feb-06 0:11 
AnswerRe: Finding Least & Max value Pin
Steve Pullan3-Feb-06 0:51
Steve Pullan3-Feb-06 0:51 
GeneralRe: Finding Least & Max value Pin
Chandan_Kr3-Feb-06 19:43
Chandan_Kr3-Feb-06 19:43 
GeneralRe: Finding Least & Max value Pin
Steve Pullan5-Feb-06 12:17
Steve Pullan5-Feb-06 12:17 
QuestionError Handleing Pin
alien viper2-Feb-06 23:40
alien viper2-Feb-06 23:40 
AnswerRe: Error Handleing Pin
Steve Pullan3-Feb-06 0:03
Steve Pullan3-Feb-06 0:03 
AnswerRe: Error Handleing Pin
malharone3-Feb-06 6:19
malharone3-Feb-06 6:19 
First of all, you are using try/catch. So i'm assuming you're using VB.net. If so, then do not use legacy VB functions like "msgbox". Use "MessageBox.Show(..)".

Secondly, your first example displays the message to the user and will continue processing statements after the "End Try". Your second example, re-throws the exceptions back to the caller.

I think both of the examples are bad practice for error handling.

Alternate for Example 1:
Try<br />
  'Do Something<br />
Catch ex As Exception<br />
  Utilities.HandleError(ex)<br />
End Try

Advantages: You have a central error handling mecahnism. So if 2 weeks from today your boss tells you to start logging the errors to a text file, you wont have to change every "catch" statement but only the handler function. --> Refactoring

Alternate 1 for Example 2:
'Do Something
Advantages: In your example, you simply rethrow an exception without adding more information to it. By doing so, you actually lose critical error information -- such as inner exception and/or stack trace -- which is vital for finding the root of the problem. So simply remove try/catch block entirely.

Alternate 2 for Example 2:
Try<br />
'Do Something<br />
Catch ex As Exception<br />
Throw New Exception("Error opening your file", ex)<br />
End Try

Advantages: The advantage of this approach is that you mask the original exception message (which could be too technical for the user to understand) with your custom message without losing the original error. You're passing "ex" as an inner error so this way the caller can access both the user-friendly message and also the stacktrace of the original exception.

Hope this helps.

- Malhar
QuestionClass to convert HTML to text Pin
gaudenzio2-Feb-06 23:40
gaudenzio2-Feb-06 23:40 
AnswerRe: Class to convert HTML to text Pin
gaudenzio3-Feb-06 5:19
gaudenzio3-Feb-06 5:19 
QuestionCorp an Image in VB 6.0 Pin
miftha2-Feb-06 21:54
miftha2-Feb-06 21:54 
GeneralRe: Corp an Image in VB 6.0 Pin
bskirkman2-Feb-06 23:07
bskirkman2-Feb-06 23:07 
Questionusing DBNull.value Pin
raj kumar reddy2-Feb-06 19:39
raj kumar reddy2-Feb-06 19:39 
AnswerRe: using DBNull.value Pin
Rana Muhammad Javed Khan2-Feb-06 20:48
Rana Muhammad Javed Khan2-Feb-06 20:48 
GeneralRe: using DBNull.value Pin
Colin Angus Mackay2-Feb-06 23:05
Colin Angus Mackay2-Feb-06 23:05 
GeneralRe: using DBNull.value Pin
Guffa2-Feb-06 21:07
Guffa2-Feb-06 21:07 
AnswerRe: using DBNull.value Pin
alien viper2-Feb-06 21:41
alien viper2-Feb-06 21:41 
QuestionDatagrid Editing Pin
mayhem_rules2-Feb-06 19:31
mayhem_rules2-Feb-06 19:31 
AnswerRe: Datagrid Editing Pin
Rana Muhammad Javed Khan2-Feb-06 20:33
Rana Muhammad Javed Khan2-Feb-06 20:33 
GeneralRe: Datagrid Editing Pin
mayhem_rules2-Feb-06 20:49
mayhem_rules2-Feb-06 20:49 
QuestionVB 2005 Express and the Pocket PC Pin
delphidab2-Feb-06 19:02
delphidab2-Feb-06 19:02 
AnswerRe: VB 2005 Express and the Pocket PC Pin
Dave Kreskowiak3-Feb-06 8:31
mveDave Kreskowiak3-Feb-06 8:31 
Questionrubber banding and tracking linked image boxes linked by rubber bands Pin
miraz9122-Feb-06 18:01
miraz9122-Feb-06 18:01 
AnswerRe: rubber banding and tracking linked image boxes linked by rubber bands Pin
Dave Kreskowiak3-Feb-06 8:33
mveDave Kreskowiak3-Feb-06 8:33 
Questionerror user defined type not defined Pin
raj kumar reddy2-Feb-06 17:41
raj kumar reddy2-Feb-06 17:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.