Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 25th, 2006, 3:55 PM   #1
hush
Programmer
 
hush's Avatar
 
Join Date: Feb 2006
Location: 127.0.0.1
Posts: 35
Rep Power: 0 hush is on a distinguished road
try - catch statements

i was wondering if people used try-catch-end try statements in there code? (not just when debugging but all the time)

is this best practice?

do pro programs use them when producing code?

when i programmed in C i did not come across this concept(to be fair i didn't do a whole lot in that language), have other languages got anything similar (i assume other .NET ones do)?

Hush
hush is offline   Reply With Quote
Old Jul 25th, 2006, 4:16 PM   #2
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
Is it best practice - No

Does it get done - Yes

If you can you should generally try to prevent the exception from being thrown, the mechanism for doing so is generally slower and will use more resources.

From a strictly practical standpoint though they're nice to use to prevent from needing to worry about all things that can go wrong. Throw an exception try/catch instead of trying to prevent everything, throw an error message based on the exception type returned, and continue as normal.

Hope this helps a bit.

-MBirchmeier
MBirchmeier is offline   Reply With Quote
Old Jul 25th, 2006, 4:33 PM   #3
JimmyJim
Programmer
 
JimmyJim's Avatar
 
Join Date: Jul 2006
Location: England
Posts: 43
Rep Power: 0 JimmyJim is on a distinguished road
try-catch-finnaly blocks are the main exception handling mechanisms in java and c# and VB.net.

In java you have to use them if the method you are calling has thows keyword in the method header. Its one of the most anoying features if you ask me as you end up with lots of try-catch blocks everywhere especially if you do IO. I know that there are arguements for compulsory try-catch blocks, but IMO it should be up the the programmer to decide if they are necessary. C# never forces you to have a try-catch block, so i guess the same is for VB.net.

IIRC C does not have try-catch blocks in the language spcification. I think there are some extensions that use macros that enable simiar functionality. C++ has try-catch blocks as part of the spec, though im sure die hard C programmers still use older styles of error handling.
JimmyJim is offline   Reply With Quote
Old Jul 25th, 2006, 4:51 PM   #4
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 371
Rep Power: 0 King is an unknown quantity at this point
I use them in C# when working with a database, because if someone messes around with the database and your program errors, it’s easy to see what the error is and your program won’t crash because of it.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Jul 25th, 2006, 4:51 PM   #5
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
I can't edit the previous post anymore so I'll add my edit here:

<edit> As jimmy noted they are in use in the code. From what I've seen it's good use to have them, so that if you do hit an exception you'll recover gracefully rather than crash. It's not good practice to rely on them for normal flow control</edit>
MBirchmeier is offline   Reply With Quote
Old Jul 25th, 2006, 10:24 PM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 998
Rep Power: 4 lectricpharaoh will become famous soon enough
I agree with MBirchmeier. They are nice to have, and go a long way to writing robust code. However, the performance is usually much slower than simply checking a flag or return value, and as such, they shouldn't be used for most operations. For example, when opening a file, it seems nonsensical to throw an exception if the file cannot be opened; instead, return a success/failure code like the C standard library does. On the other hand, if the file was successfully opened, but somehow became invalid (say, it was on a network-mapped drive, or another task deleted the file), throwing an exception seems like a good idea.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jul 26th, 2006, 12:18 PM   #7
bigguy
Professional Programmer
 
bigguy's Avatar
 
Join Date: Sep 2005
Location: Arkansas
Posts: 296
Rep Power: 0 bigguy is an unknown quantity at this point
Send a message via AIM to bigguy Send a message via MSN to bigguy Send a message via Yahoo to bigguy
I use them ofr exceptions. Like if I make a calculator, I only allow numerical characters in teh boxes, and not letters, or if letters are found. Make their value = 0
__________________
Forgiveness is the fragrance that the violet sheds on the heal that has crushed it. - Mark Twain

Destruction leads to a very rough road, but it also breeds creation.
bigguy is offline   Reply With Quote
Old Jul 26th, 2006, 1:51 PM   #8
randum77
Programmer
 
randum77's Avatar
 
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3 randum77 is on a distinguished road
Quote:
Originally Posted by lectricpharaoh
For example, when opening a file, it seems nonsensical to throw an exception if the file cannot be opened; instead, return a success/failure code like the C standard library does
Had i not read this, I would of never thought of this. Great idea!
__________________
_Marshall_

"America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt."
randum77 is offline   Reply With Quote
Old Jul 27th, 2006, 11:20 PM   #9
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 371
Rep Power: 0 King is an unknown quantity at this point
Quote:
Originally Posted by lectricpharaoh
For example, when opening a file, it seems nonsensical to throw an exception if the file cannot be opened; instead, return a success/failure code like the C standard library does.
Would you happen to know how I can do this in C#, if it does at all?
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Jul 28th, 2006, 12:33 AM   #10
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 236
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
i'd use the IOException class

you could do it like this(i know this is a directory but same concept)
DirectoryInfo dir = new DirectoryInfo(@"F:\WINNT");
try
{
  dir.CreateSubdirectory("Sub");
  dir.CreateSubdirectory(@"Sub\MySub");
}
catch(IOException e)
{
  Console.WriteLine(e.Message);
}

(I stole this code from here: http://www.codeguru.com/Csharp/Cshar...cle.php/c5861/)


OR here's an example of sorting out the IOException
http://www.java2s.com/Code/CSharp/La...OException.htm
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA-if statements Quagmire Visual Basic 3 Feb 17th, 2006 2:44 PM
Catch command line output SoniX Visual Basic .NET 1 Jan 22nd, 2006 6:25 AM
Try without catch JavaDummy Java 4 Jul 30th, 2005 4:20 PM
Multiple IF statements, perhaps? foxcity911 PHP 7 Jun 22nd, 2005 3:33 PM
Code annoyance, variables not available outside of if statements? Arla C# 15 May 24th, 2005 8:53 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC