![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2006
Location: 127.0.0.1
Posts: 35
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 3
![]() |
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 |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jul 2006
Location: England
Posts: 43
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 371
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 3
![]() |
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> |
|
|
|
|
|
#6 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 998
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#7 |
|
Professional Programmer
|
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. |
|
|
|
|
|
#8 | |
|
Programmer
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3
![]() |
Quote:
__________________
_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." |
|
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 371
Rep Power: 0
![]() |
Quote:
__________________
I am Addicted to Linux! |
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |