By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,694 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,694 IT Pros & Developers. It's quick & easy.

Error Handling

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Justin Dutoit" <an**@anon.com> wrote in
news:#D*************@tk2msftngp13.phx.gbl:
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 : )

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #2

P: n/a
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
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@140.99.99.130...
"Justin Dutoit" <an**@anon.com> wrote in
news:#D*************@tk2msftngp13.phx.gbl:
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 : )

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 20 '05 #3

P: n/a
"Justin Dutoit" <an**@anon.com> wrote in
news:OD**************@TK2MSFTNGP10.phx.gbl:
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.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #4

P: n/a
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
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@140.99.99.130...
"Justin Dutoit" <an**@anon.com> wrote in
news:OD**************@TK2MSFTNGP10.phx.gbl:
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.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 20 '05 #5

P: n/a
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.

-------

Nov 20 '05 #6

P: n/a
Justin,
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.<<

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.

"vipul DotNet" <yp***@hotmail.com> wrote in message
news:OY**************@TK2MSFTNGP09.phx.gbl... 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.

-------


Nov 20 '05 #7

P: n/a
Cor
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
Nov 20 '05 #8

P: n/a
nice chap ,
yes this code will throw an error and it will be caught under the
system.OverflowException

"Nice Chap" <Ni******@PlasmaDyne.com> wrote in message
news:#O**************@TK2MSFTNGP11.phx.gbl...
Justin,
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.<<

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.

"vipul DotNet" <yp***@hotmail.com> wrote in message
news:OY**************@TK2MSFTNGP09.phx.gbl...
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.

-------



Nov 20 '05 #9

P: n/a
thanks Nice Chap for the warning !

vipul

"Nice Chap" <Ni******@PlasmaDyne.com> wrote in message
news:#B**************@TK2MSFTNGP10.phx.gbl...
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.

"vipul DotNet" <yp***@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...
nice chap ,
yes this code will throw an error and it will be caught under the
system.OverflowException

"Nice Chap" <Ni******@PlasmaDyne.com> wrote in message
news:#O**************@TK2MSFTNGP11.phx.gbl...
Justin,

>>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.<<

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.

"vipul DotNet" <yp***@hotmail.com> wrote in message
news:OY**************@TK2MSFTNGP09.phx.gbl...
> 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.
>
> -------
>
>
>
>
>



Nov 20 '05 #10

P: n/a
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
Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.