Forum Moderators: coopster

Message Too Old, No Replies

Exception Handling a sign of sloppy coding?

         

eelixduppy

11:44 pm on Jan 29, 2008 (gmt 0)



Although the following quote is from php.net, I'd like to make this a conversation of general programming and not specific to any one language.


Code may be surrounded in a try block, to facilitate the catching of potential exceptions.

[php.net...]

My question to you is, why would anyone want to allow the possibility that an exception can occur in your code? Don't you think that necessary checks and validation should be made before you do something? In my opinion knowing that an exception may be thrown and then doing nothing to prevent it from happening, but instead handling it when it does happen, is sloppy design/coding and should be avoided. This is mainly why I do not use exception handling; I am paranoid and check my data before performing an operation on it and I would encourage everyone to do the same.

Your thoughts? Similar? Different? How many of you actually use exception handling to its fullest potential when you implement a project? Just curious and I'd like others' opinions :)

LifeinAsia

12:01 am on Jan 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



In the world of XML and RSS feeds from multiple sources, it is ridiculous to expect that everyone else will provide perfectly formed data every time. Rather than code in thousands of exceptions for every single possible visioned malformation (and still end up missing some), exception handling is much more efficient (in terms of code generation and processing of the actual code).

Instead of thousands of IF..THEN or CASE statements, 1 or 2 exception handlers can provide the same functionality without hours of coding and without the overhead to process all those statements.

cameraman

12:50 am on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not going to go do the research first so I may be completely wrong here, but I believe the exception paradigm was borne from event handling for hardware where so much happens asynchronously - for example, testing to see if an A/D converter is ready to convert a sample from analog to digital, display card rendering events, sound processing, etc. and again I could be misremembering but I believe it was a hardware feature of the 386 processor.

It appears to me that it has evolved as a structured way of handling unknown circumstances. Like many other technologies available to us as programmers, it can be used "properly" or it can be abused into a mess that no one wants to look at or maintain. As LifeInAsia touched on, it's a way [if laid out "properly"] to localize and generalize handlers that would have to do pretty much the same thing.

I agree with your assertion that you should validate what you're doing before you do it - I believe it's by definition part of producing a robust application. I haven't seen an instance in php that I've needed to use exceptions, but then again I've not done much with XML or anything with RSS from php. As I recall, I was barely starting to get into it when I stopped writing in C++. I'm not sure I wholly agree that it's an indication of sloppy programming. I think that if it's part of the plan, and I stress the word plan, then it can be utilized as a structured way of handling scenarios that the app might potentially encounter (unsloppily as it were <grin>).

I'm far, far from being an expert javascripter; to my albeit limited knowledge there is no way to gracefully test for an undefined variable without try...catch. Same for XML from javascript - it gets right ticked off if you try to do anything with an empty tag. It's also arguably a more robust way to test for IE/version/not IE when you're trying to set up an ajax conversation. I do have to say that IMHO these are necessary due to shortcomings in the language.

I definitely understand what you're saying, but overall I think it has its place.

eelixduppy

1:02 am on Jan 30, 2008 (gmt 0)



I understand what you both are saying, and I mostly agree. The arguments you gave, however, are speaking almost entirely of "checked exceptions". While that is ok, because I didn't specify, I was more leaning towards "unchecked exceptions" handling. For those that don't know the difference here is a quick definition:

unchecked: A defect in the program due to program logic - essentially a bug.
checked: Invalid conditions that happen outside the scope and control of the application, such as network failure, missing files, etc...

btw, I believe this terminology is used for Java, however, it is still relevant

At times, I understand that you cannot predict an unchecked exception all the time, so to catch the exceptions thrown would be a reasonable solution to that problem. But what of that of checked exceptions.

LifeinAsia

1:14 am on Jan 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



By your definitions, then yes, I'd say that the purpose of exception handling should be to handle unchecked exceptions. Relying on exception handling to CYA for buggy code is definitely sloppy.

However, I will sometimes use exception handling during *development* to handle bugs. For example, I know about bugs A and B, but I need to fix the main issue C before I can work on A and B, so I need to use exception handling to takje care of A & B so the program (or page or whatever) gets to the point where I can work on C. But the issue is that I will (hopefully :) ) go back and fix bugs A & B before pushing things into *production*.

Um, tring to think of a good example...
OK, maybe not a great example, but let's say a partner provides content in XML. Most of the time, it's in format 1, but 5% of the time it's in format 2. I need to go forward and get the layout for format 1 content finalized so it can be approved by management. So I'll use exception handling to just ignore any content in format 2 for now. Once the layout is done for format 1, I send that to management for approval. Meanwhile, I go back and work on parsing format 2.

cameraman

8:28 am on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure I like those definitions, but accepting them as true for the purpose of discussion...

It looks to me like unchecked exceptions are exactly as you posit in your original post - it's using the mechanism to cover for defective, sloppy programming. I would say its only saving grace in this case is that something is better than nothing; attempting recovery with perhaps urls to click or to display "uh, we'll have to get back to you on that" is at least better than a black on white 'divide by zero error on line x of script' sort of thing where the only recovery is someone hitting a Back button (and watching it happen again :o ).

I think it's a valid method of handling checked exceptions, though, with the qualifications I stated above. Often your own code is throwing the exception in order to provide a consistent way of noting and/or recovering from the offending conditions. And, as mentioned, sometimes it's the only way to recover.

Using it during development is a completely different animal and is an excellent use which I may just have to explore!

henry0

12:49 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that W3c [w3schools.com] has made a nice job in explaining exception
It shows the proper way to handle exceptions
The email example is a good example
However my question is why through an excep when your code should catch it anyway? And if so what should be exceptions real need for?
I’ll like getting a better understanding of why using excep rather how…

LifeinAsia

4:34 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



However my question is why through an excep when your code should catch it anyway?

As I previously mentioned, having to code for every possible exception leads to code bloat and increased processing requirements. And most likely something will still slip through the cracks. (Look at any MicroSoft program- SERIOUS code bloat and you can still cause it to crash non-gracefully.)

Yes, in some cases you can (and should) code all the exceptions. For example, user inputs 2 numbers a & B and your program does the simple task of computing A/B. WIthin the code you should handle:
- B=0
- A and/or B is non-numeric
- A and/or B is not entered
But that is a very simple example.

henry0

6:26 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So “exception” is really defined by exception.
I mean it just consists in handling errors/input errors etc…
I was confused… I thought it means another form of error handling that I was not familiar with.
As such it is not only valuable but a must have in case the script relies on external (to the script vars)
<<<
My question to you is, why would anyone want to allow the possibility that an exception can occur in your code?
>>>
If the script does not rely on “external” input and is buggy then any exception would be pretty useless for by all means an error will occur.
Although it seems to be a good idea to add a few exit() with a decent message here and there just in case…
Like conn(), time out problem etc

eelixduppy

7:45 pm on Feb 3, 2008 (gmt 0)



Thanks for your comments, everyone. Much appreciated :)