Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   try - catch statements (http://www.programmingforums.org/showthread.php?t=10843)

hush Jul 25th, 2006 3:55 PM

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

MBirchmeier Jul 25th, 2006 4:16 PM

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

JimmyJim Jul 25th, 2006 4:33 PM

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.

King Jul 25th, 2006 4:51 PM

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.

MBirchmeier Jul 25th, 2006 4:51 PM

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>

lectricpharaoh Jul 25th, 2006 10:24 PM

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.

bigguy Jul 26th, 2006 12:18 PM

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

randum77 Jul 26th, 2006 1:51 PM

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!

King Jul 27th, 2006 11:20 PM

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?

melbolt Jul 28th, 2006 12:33 AM

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


All times are GMT -5. The time now is 1:46 AM.

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