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

The Try Catch

P: n/a

Does someone have a good site I can visit or explain the use of Try" and
Catch foe exception/error handling. What is the logic behind this
command and maybe an example would be great!!!!
Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
http://www.vbdotnetheaven.com/Code/Jun2003/2009.asp

Hope that helps,

Greg

"Michael MacDonald" <ma***********@yahoo.com> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...

Does someone have a good site I can visit or explain the use of Try" and
Catch foe exception/error handling. What is the logic behind this
command and maybe an example would be great!!!!
Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #2

P: n/a
Just in case you don't want to register for that site in the previous
posting, here's my 2c on the issue:

Try
' Some code that you think might cause an error
Catch ex As Exception
' do something here that you would have done with an
' On Error Goto
' in VB6
End Try

You can also replace the Catch block with a finally block. The finally block
executes wether an exception occurs or not.

"Michael MacDonald" <ma***********@yahoo.com> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...

Does someone have a good site I can visit or explain the use of Try" and
Catch foe exception/error handling. What is the logic behind this
command and maybe an example would be great!!!!
Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #3

P: n/a
* Michael MacDonald <ma***********@yahoo.com> scripsit:
Does someone have a good site I can visit or explain the use of Try" and
Catch foe exception/error handling. What is the logic behind this
command and maybe an example would be great!!!!


<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconTryCatchStatements.asp>
+ links on this page

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

P: n/a
Is the try,catch,finally a common necesity in VB.Net programming. I see
from one of the links it could be common in the divide by zero scnario
but do you guys use "Try" regularly? Another question when you do use
the try statment does it try the line of code when the program loads or
when a function is called or well when does it "try" that potentially
error prone line of code???

Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5

P: n/a
Michael,
From a previous question I answered on Try/Catch
<quote>
I am using allot the try catch in my code and the question is
if it is good ? I would favor global exception handlers over a lot of Try/Catch statements,
however I have a number of Try/Finally statements to ensure resources are
disposed of properly. In other words: Try/Finally good, Try/Catch not so
good. See MSDN article below.
it will decrease my performance ? A Try statement does not decrease performance, the act of throwing &
handling an exception will. In other words if you never throw an exception
there is no performance impact.
is it good to use the exit function in the catch section in case of some
problem (after writing the error in to some log) ? If I had a problem in a Catch block, I would allow an exception to be raised
that would be handled by a higher catch block or one of my global exception
handlers.

</quote>
Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay
"Michael MacDonald" <ma***********@yahoo.com> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
Does someone have a good site I can visit or explain the use of Try" and
Catch foe exception/error handling. What is the logic behind this
command and maybe an example would be great!!!!
Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #6

P: n/a
Thanks for the informative explanation. I will try using "Try" in my
next homework assignment and see if the proffesor notices I gave a
little extra effort! Whatever helps to get the "A" I always say.

Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7

P: n/a
There's already a built-in global exeption handler... we just don't get to
see it when debugging. At runtime, when an unhandled exception occurs, the
runtime gives the user the option of continuing gracefully. I would argue
that .NET (at least VB.NET) already has global exception handling... you
don't need to add your own unless you want to record a log file or something
like that.

Anyway, in terms of when to use Try/Catch, I tend to feel that you only need
to use Try Catch for *expected* errors (yes, you read that correctly) and
when resource cleanup is essential. All other exceptions can be safely
bubbled up to the runtime or to your own global exception handler.

For instance (pseudocode mostly):

Dim fs as FileStream("C:\My File")

Try
fs.Write("somedata")
Catch
'do nothing
Finally
fs.Close
End Try

If you don't use the Try in the above case, if the Write statement throws an
error, then your file will remain open until your app ends. A similar
situation can occur with database connections and datareader objects.

P.S. On a similar note, IMHO, I think a lot of the classes in the framework
overuse exceptions. For instance, String.Substring raises all sorts of
exceptions if not used in precisely the right way (i.e.
String.Substring(0,10) on 5 character string will raise an exception) which
is why I prefer to still use VB's intrinsic and very graceful Mid/Left/Right
statements. There's no beating those classic B.A.S.I.C. statements.
"Michael MacDonald" <ma***********@yahoo.com> wrote in message
news:up*************@TK2MSFTNGP10.phx.gbl...
Thanks for the informative explanation. I will try using "Try" in my
next homework assignment and see if the proffesor notices I gave a
little extra effort! Whatever helps to get the "A" I always say.

Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #8

P: n/a
Hi Michael,

In addition to all the others,

The Try, catch, end Try Finally is to catch errors which can not be traped
in your own logic.

Think for that about file which did exist however is erased in the mean
time before or while you processes it. By instance because of the fact that
somebody disconnected the CD player.

You do not have to catch if a value is zero, that you can test, you also do
not have to catch if the power is down.

And as last

Try
Try
Catch
End Try
Catch ioexeption
Catch normal exeption
Finally
End Try

Is as well a very often used situation.

I hope this helps?

Cor
"Michael MacDonald" <ma***********@yahoo.com> schreef in bericht
news:eU**************@TK2MSFTNGP10.phx.gbl...

Does someone have a good site I can visit or explain the use of Try" and
Catch foe exception/error handling. What is the logic behind this
command and maybe an example would be great!!!!
Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #9

P: n/a
it's something commonly used across all programming languages, it originated
in C++ with the try, catch blocks
"Michael MacDonald" <ma***********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Is the try,catch,finally a common necesity in VB.Net programming. I see
from one of the links it could be common in the divide by zero scnario
but do you guys use "Try" regularly? Another question when you do use
the try statment does it try the line of code when the program loads or
when a function is called or well when does it "try" that potentially
error prone line of code???

Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.