Error Handling

  • Thread starter Thread starter Justin Dutoit
  • Start date Start date
J

Justin Dutoit

Hey. I'm still not experienced at error handling, and I need to know if
Try.. Catch blocks are meant to be used to handle errors in your own app, ie
bugs. Or, are they only for external things like db problems, file not
found. Any websites or books on error handling, real-life examples, would be
appreciated.

Tks

Justin Dutoit
 
Hey. I'm still not experienced at error handling, and I need to know
if Try.. Catch blocks are meant to be used to handle errors in your
own app, ie bugs. Or, are they only for external things like db
problems, file not found. Any websites or books on error handling,
real-life examples, would be appreciated.

Try Catch is used to handle ANY error... could be DB error, your own
errors, etc.

As the name of the syntax implies, it is used to catch errors : )
 
Could you point me to some detailed examples, including how the errors are
handled? I know you log them in the db, or windows event log, but what
else....

Tks
Justin
 
Could you point me to some detailed examples, including how the errors
are handled? I know you log them in the db, or windows event log, but
what else....

Text File. Display message box. It doesn't really matter. It's up to you
how to handle the error.

Some people even call another function and process the data a different
way.

Think of the Try, Catch as a big if statement, with the condition being the
error.
 
Thanks, but I mean an example of a situation, say, editing a record in a db.
What can go wrong and what might the code look like please?

Tks
Justin
 
this is a simple example :-

private sub errHandler()

Dim x As Integer

Dim y As Integer

Dim z As Integer

x = 10

y = 0

Try

'we are going to divide and integer by zero to generate an error

z = x / y

Catch ex1 As System.OverflowException

MessageBox.Show("Over Flow")

Finally

MessageBox.Show("finally")

End Try

this code will throw the error.

-------
 
Justin,
bugs. Or, are they only for external things like db problems, file not
found.<<

If I understand your question correctly, here is my tuppence worth:

1. Try Catch are used to handle 'Exceptions'
2. Exceptions as 'Unexpected' events that prevent a method/function from
performing its nomral task.
3. Expections should not be confused for Application Rule Failure
(Validation). You except the validation to fail and you would know how to
handle it.
4. So, 'Your' methods always should return an Error Code to indicate success
or failure.
5. Catch an exception ONLY when you know how to handle it.
6. For predictability, it is a good idea to install a global Expception
Handler to handle all Unhandled exceptions.

Well about how exactly to handle Exceptions, when you are using the services
of an external dotnet object(some thing written by others), the author will
normally publish the Exceptions that the object is programmed to throw. You
handle those exceptions in your function( only if you know how to deal with
it).

Vipul,

Are your sure z = x / y will cause an Exception ? Dotnet uses IEEE
Arithmetic standard and I would be surprised if divide by zero is thrown as
an exception in VB.net.
 
Justin,
Hey. I'm still not experienced at error handling, and I need to know if
Try.. Catch blocks are meant to be used to handle errors in your own app, ie
bugs. Or, are they only for external things like db problems, file not
found. Any websites or books on error handling, real-life examples, would be
appreciated.
If you try this little example, I think you see it.
\\\
Dim a As Integer
Try
a = CInt("a")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
/////
I hope this helps a little bit.
Cor
 
nice chap ,
yes this code will throw an error and it will be caught under the
system.OverflowException
 
thanks Nice Chap for the warning !

vipul

Nice Chap said:
Well, here is a word of caution, if you used 'Single' or 'Double' as the LHS
of a quotient assignment( in most of the cases you would), dot net DOES NOT
THROW AN EXCEPTION for divide by zero. You have to use IsInfinity* functions
to test the value for your range. This is in line with IEEE Arithmetic. But
If you used an integral data type as LHS dotnet does throw an exception.
 
Hi Nice Chap,

I'll just pick up on one item in your list:

|| 5. Catch an exception ONLY when you know how to handle it.

I think I know what you mean but I would put it as

5. Catch an exception when you KNOW how to handle it AND
catch an exception ESPECIALLY when you DON'T know how to handle it.

Lol. I don't think I'm actually contradicting you, more correcting. ;-)

You catch an exception when you expect it and <can> deal with it. Then you
do whatever's necessary and carry on afterwards. That's like when Lucas said
it's a big 'If'.

You also catch them when you know to how to tidy up but can't carry on and
have to pass the buck.

Try
Try
Something (Args)
Catch e1 As AnExceptionThatsOkByMe
DoSomeRecovery (MoreArgs)
Finally
CarryOn (YetMoreArgs)
End Try

Catch e2 As ExpectedExceptionThatsBeyondMe
CleanUpAsFarAsPossible (CleanUpArgs)
Dim Mess As String = "Aaargh - THIS happened"
Dim Up As New Exception (Mess)
Throw Up

'For totally unexpected exceptions
Catch BadAssError As Exception
CleanUpAsFarAsPossible (CleanUpArgs)
Dim Mess As String = "Aaargh - Something WIERD happened"
Dim Up As New Exception (Mess, BadAssError)
Throw Up

Finally
Whatever (Necessary)
End Try

All that code just to do Something(). :-(

Regards,
Fergus
 
Back
Top