![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Firstly, the performance overhead would only make a significant difference if one were expecting exceptions to be thrown on a regular basis. If you throw exceptions only in exceptional circumstances (as the name "exceptions" suggests), then any performance degredation is going to be negligible at best. Further, exceptions have several advantages over return values. From a structural point of view, they cleanly separate the concept of return values from errors and warnings. From a practical point of view, they automatically propagate up the stack until they are caught, so the programmer need only deal with the error once, rather than construct an elaborate chain of return values, or a singleton/global variable to hold error information. Finally, languages like Java throw compile errors at uncaught exceptions, preventing possible bugs. In any unusual or exceptional circumstance, exceptions should be used. Return values should not, in my opinion, be used for anything other than returning valid data. |
|
|
|
|
|
|
#12 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 4
![]() |
Quote:
The problem is (especially when learning to use exceptions) there's a tendancy to overuse them, say in the example of opening a file, the following shouldn't be exceptions: The file not existing The file being locked The file not being the format expected Not being able to write to the file The following could be exceptions: The file dissappearing or being deleted after being opened Outside failures in the middle of writing (eg. hard drive fills up) I've seen too many instances of code where people use exceptions as flow control to see if a file exists. If it's writeable etc. If they truly are exceptional cases, by all means try/catch/finally them, however, if it's something that can easily be checked programatically then by all means do so. -MBirchmeier note: the performance loss of a try/catch is usually minimal compared with customer frustration at a program that crashes. It should be common practice to try/catch any code that might be expected to throw exceptions, but they should only be thrown when the outside envrionment does something unexpected, not because your program did something unexpected such as divide by 0, or try opening a file that doesn't exist. |
|
|
|
|
|
|
#13 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Now, if one were doing work with threads, then one might expect files to be found locked regularly, and because it is expected, one would probably have some function to check whether the file is locked rather than handle it via an exception. Likewise, if one wishes to write to a file that may not yet exist, then one may specify an option to create the file, if it doesn't already exist, rather than to handle it with exceptions. But this only applies when the difficulty is a common and expected occurance. When the difficulty is unexpected, such as with your third example ("The file not being the format expected"), then you should certainly use an exception. Indeed, I'd go so far as to say it's bad practise not to use one. The other factor to consider is the handling of unexpected events. If I have an application that accesses a file, and that file turns out to be in an invalid format, then it's logical that I report said error to the user. But since my program is clean and modular, I can't pollute my document opening function with GUI specific code. Likewise, because I happen to be using a statically typed language, I can't return an error message when a document is expected. So I either have to wrap up the document in an error handling layer, or I have to record the error in a global variable that can be polled when necessary. In short, I have to create my own exception handling routines and reinvent the wheel. And reinvent it badly, as it happens, since neither solution has all the benefits that exceptions have. Thus, in any case where non-standard behaviour occurs, exceptions are the best way to handle it. |
|
|
|
|
|
|
#14 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 4
![]() |
Ironically enough for the same reasons I tend to disagree.
For these types of problems, locked files, invalid formats, etc. They're generally something that's easy to check for. In my world the user entering filenames that might not exist, a text or XML not being of the expected format, or not having write access are all considered common occurances. It's just something that should be checked. Programatically handling an error can be easily done with an error handler without breaking modular code. (Generally something I consider a utility anyway) You call your error utility to notify where and what type of error occured, (and the severity,(silent, log, popup to the user, critical))the error handler knows how to handle the GUI, (potentally even waiting for user input) and you can continue on with execution as normal. Note @Arevos: Googling for: When to use exceptions : comes up with a blog with a point of view similar to your own Blog Linky Googling for : Proper use of exceptions: comes up with an article similar to my point of view Article Linky They're both good reading, and might give everyone a better idea of what exceptions should be for. -MBirchmeier |
|
|
|
|
|
#15 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Whilst I can't think of any advantages to the above approach, I do think it suffers from three main disadvantages: it's nonstandard, doesn't support stack transversal, and lacks compile time checking in languages which have that. |
|
|
|
|
|
|
#16 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 4
![]() |
Quote:
No stack transversal: True, but raising an event can implement similar functionality, across not only the current thread, but others as well. One datasource might be updating one or more pages or controls, raising an event notifies all reliant parts what's going on, not just those on the direct stack. Additionally I was taught that traversing a stack is a bad way to get things done unless it's absolutely necessary. If you're moving more than a layer off of the stack, expected closing methods or routines that should be finalized might not run if the coder of an intermediate layer hadn't planed for an exception. Lacks compile time checking: I don't see how so. Can you give an example of this? -MBirchmeier |
|
|
|
|
|
|
#17 | |||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
vbnet Syntax (Toggle Plain Text)
Quote:
Quote:
java Syntax (Toggle Plain Text)
|
|||
|
|
|
|
|
#18 | |||
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 4
![]() |
Quote:
Quote:
Quote:
In .NET this isn't always the case. You can have external (and even non managed) code in the stack. If used to relying on exceptions, exceptions that pass through non-managed code can have unexpected results, yes good practice says that you should catch any exceptions before moving boundaries from managed to un-managed, but often times I've seen coders neglect this recommendation, either out of ignorance, or out of (usually undeserved) trust in the non-managed code. -MBirchmeier I know we're starting to drift a bit off topic (I hope no one minds because I think it's getting interresting) if we're too far off topic though perhaps we should move this to PMs *: This level of seperation might seem excessive, but there's reasoning behind it. We do a lot of assembly line and Device related operations. Which means we get in data from a lot of different sources in a lot of different formats, (open ports, serial feeds, t/ftp, databases, snmp etc) however the data contains the same pertinant information. The Logic Layer is (usually) independant as possible, only relying on interface information from the data layer to collect the raw data, and manipulate it as necessary. The UI layer relies on interfaces from the logic layer, but different installations require different UI requirements. (the util provides cross layer utilities such as error handling, so that a problem in the DataLayer can provide notification in the UI layer if necessary, without needing to muck up the Logic Layer's execution.) This means that (hopefully) at a new installation we need to write data extracters to collect their data, possibly a custom UI depending on what they want, and the rest is gravy. |
|||
|
|
|
|
|
#19 | |||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Quote:
A more modular approach would be to catch the raised exceptions and wrap them up into an event system. This would decouple your error handling from your data layers entirely, whilst still allowing you to handle errors through globally distributed events. The advantage with this approach, over the approach you prescribe, is that only the functions that have to be are tied into the framework. This allows one to detatch any method not specifically part of the layer framework you describe. Quote:
|
|||
|
|
|
|
|
#20 | |||
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 4
![]() |
Quote:
Quote:
Quote:
-MBirchmeier |
|||
|
|
|
![]() |
| 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 3:44 PM |
| Catch command line output | SoniX | Visual Basic .NET | 1 | Jan 22nd, 2006 7:25 AM |
| Try without catch | JavaDummy | Java | 4 | Jul 30th, 2005 5:20 PM |
| Multiple IF statements, perhaps? | foxcity911 | PHP | 7 | Jun 22nd, 2005 4:33 PM |
| Code annoyance, variables not available outside of if statements? | Arla | C# | 15 | May 24th, 2005 9:53 AM |