473,503 Members | 12,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The Try Catch


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
9 3355
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
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
* 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
30251
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
6864
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
4332
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
3460
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
3694
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
3038
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
6083
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
23
2292
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.