Extremely exceptional code; or abuse of exceptions in PHP

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Extremely exceptional code; or abuse of exceptions in PHP

Postby Pesto » Tue Mar 01, 2011 2:11 am UTC

I'm working on a severely over-engineered system, where everything is broken into way too many classes, which interact with each other by changing the values of each others' public variables :x In the course of trying to track down an error, I ran across this pattern.

Code: Select all

try {
    // lots of code
} catch(Exception $e) {
    //do something
    throw new Exception();
}

The problem is that this obfuscates what part of the try block caused the exception. Easily solved, by changing it to this.

Code: Select all

try {
    // lots of code
} catch(Exception $e) {
    //do something
    throw $e;
}

Hooray! Oh, wait. Did I forget to mention this pattern was repeated three times, requiring me to make the same edit before actually tracking down the culprit?

So, here's my question. Is there ever a circumstance where you would want to catch an exception, only to throw a new exception?

In one of the cases I found in the code, throwing an exception from the catch block was completely appropriate. The problem was that a new exception was thrown instead of rethrowing the caught exception. For example, here's some completely appropriate code.

Code: Select all

try {
    start_db_transaction();
    // lots of code
    end_db_transaction();
} catch(Exception $e) {
    rollback();
    throw $e;
}

And, yes, I'm working in PHP. :cry:
Last edited by Pesto on Tue Mar 01, 2011 7:33 pm UTC, edited 2 times in total.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Extremely exceptional code

Postby Yakk » Tue Mar 01, 2011 3:02 am UTC

How are the exceptions being used? If the exceptions are being used as "documented crashes", then catching and throwing a new one just gets in the way (unless you instrument the second throw with even more useful information than the first).
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: Extremely exceptional code

Postby Pesto » Tue Mar 01, 2011 3:35 am UTC

I don't know that the exceptions are "used" for anything.

I was modifying some code, which caused an exception to be thrown in some deep dark corner of the system. One of the existing tests failed and told me precisely from which file and on which line the exception was thrown. The only problem is that because exceptions were being caught and new exceptions being thrown, the test framework directed me to the exception three levels above where the actual error was.

In this case, the most valuable information is the line on which the original exception was thrown.

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Extremely exceptional code

Postby phlip » Tue Mar 01, 2011 4:10 am UTC

Java has chained exceptions, which allow you to "wrap" one exception inside another... the main use for which is to change the exception class (eg you have a library that has to read from a database... when you try to open the database file it's missing, so it throws an IOException... the database layer then catches that and wraps it in an SQLException or similar, then your library catches that and wraps it in a MyLibraryException("Couldn't connect to database") or something). But that lets you see the full stack trace of the original exception.

I imagine that the reasoning behind the catch-and-rethrow you have there is similar... I don't know much about exceptions in PHP (most PHP code uses C-like return values rather than exceptions). But I see in the docs that it does do exception chaining... you just have to pass the exception you caught into the new exception's constructor.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Extremely exceptional code

Postby Berengal » Tue Mar 01, 2011 5:13 am UTC

Chained exeptions is pretty much the way to go almost everywhere you want to encapsulate an exception. I can only think of one exception (heh), and that's when you're serializing exceptions. There can be trouble if the serialized exception cannot be deserialized again (e.g. when you're sending it to another process that doesn't have the libraries loaded that you have) or if the exception isn't serializeable. If you're serializing exceptions, make sure all exceptions you serialize are part of a documented API and made to be serializeable. There are several ways to deal with these cases, for example logging the exception properly before discarding it, or even copying over the message and stacktrace into another exception. The golden rule is never discard an exception without printing the message and the stacktrace.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: Extremely exceptional code

Postby Pesto » Tue Mar 01, 2011 7:43 am UTC

Looks like PHP got chained exceptions in version 5.3. We haven't upgraded to that yet.

phlip wrote:I imagine that the reasoning behind the catch-and-rethrow you have there is similar...

No, I think ignorance was responsible. By throwing a new exception all information about the caught exception was being discarded.

Oh, another nice thing. Exceptions are being explicitly handled in test cases like this.

Code: Select all

testFoo() {
    try {
        //code testing foo
    } catch(Exception $e) {
        assertTrue(False);
    }
}

Except that the test framework already handles exceptions by catching them, then telling where they came from. This completely obfuscates any of that useful information.

All the people who wrote this code have moved on. Some voluntarily, some not. God, I hope to never leave a legacy like this.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Extremely exceptional code

Postby Yakk » Tue Mar 01, 2011 12:46 pm UTC

Did the test framework exist when the code was written?

Is that the only framework in which the code was written?
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: Extremely exceptional code

Postby Pesto » Tue Mar 01, 2011 5:52 pm UTC

Yes and yes.

gorcee
Posts: 1501
Joined: Sun Jul 13, 2008 3:14 am UTC

Re: Extremely exceptional code

Postby gorcee » Tue Mar 01, 2011 7:17 pm UTC

I thought this thread was about really good code.

:(

User avatar
psykx
Posts: 408
Joined: Sat Feb 23, 2008 11:24 pm UTC
Location: England
Contact:

Re: Extremely exceptional code

Postby psykx » Wed Mar 02, 2011 11:25 am UTC

gorcee wrote:I thought this thread was about really good code.

:(

no such thing...
Berengal wrote:Only if they're killer robots. Legos are happy robots. Besides, even if they were killer robots it wouldn't stop me. You can't stop science and all that.

User avatar
MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

Re: Extremely exceptional code; or abuse of exceptions in PH

Postby MHD » Wed Mar 02, 2011 4:50 pm UTC

If there only existed test for how good people was a coding a specific language... Then you could hire only sufficiently good programmers.
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"

User avatar
psykx
Posts: 408
Joined: Sat Feb 23, 2008 11:24 pm UTC
Location: England
Contact:

Re: Extremely exceptional code; or abuse of exceptions in PH

Postby psykx » Thu Mar 03, 2011 11:44 am UTC

MHD wrote:If there only existed test for how good people was a coding a specific language... Then you could hire only sufficiently good programmers.


This is fairly variable though, it depends on the technology used, the management environment e.g agile vs xtream vs none etc.., the style of code, and whether or not your starting from scratch.
Berengal wrote:Only if they're killer robots. Legos are happy robots. Besides, even if they were killer robots it wouldn't stop me. You can't stop science and all that.

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Extremely exceptional code; or abuse of exceptions in PH

Postby Emu* » Fri Mar 04, 2011 4:44 pm UTC

psykx wrote:e.g agile vs Extreme vs none etc..,

FTFY
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests