473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Error Handling

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
10 2713
"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
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
"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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
1
by: Metal Dave | last post by:
I do not understand the error handling of SQL Server here. Any error in bulk insert seems to halt the current T-SQL statement entirely, rendering it impossible to log an error. The first statement...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
1
by: pob | last post by:
>From a form I have some code that calls 4 modules frmMain 1 mod 2 mod 3 mod 4 mod If mod 1 experiences an error the error handling works fine within mod 1 and writes out the error to a...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.