Re: Exceptions - what do they mean?



I'm working on a class where I might want to throw some exceptions.
Now I'm wondering they why's and whens of exception throwing. I have
several exceptions that could happen on __construct, some of which are
more like warnings, or state notifications. Is this the proper use of
an exception?

If you throw in exception in __construct, you have failed to create
the object. That sounds a bit more serious than a warning to me.
More like an ERROR.

I guess my question is, what does an exception communicate to the
client programmer? Does throwing an exception mean that something has
gone horribly wrong, and the object is thereby unusable?

The object was not created if the constructor threw the
exception. That's worse than unusable. If you tried to assign
the object to a variable, that assignment never happened, so
that variable might contain a previous object.

If it's some other method throwing the exception, I suppose you
could use exceptions for warnings. Whatever return value the
method is supposed to have, it won't return it.


The one thing
I do know about exception behavior is that if they aren't caught, then
the client programmer gets warnings popping up that they don't want.

Except for stuff you really can't do anything about (e.g. bad disk
sectors), catch and handle your exceptions.

In a lot of examples I found, they die on exception. I got the
following code from google code, and here they die on an exception. It
looks like they do so because the whole rest of the page hinges on the
usage of the object.

If this is a web-based application, failure to create the object
should present a nice error message to the user, log an error for
the webmaster/ programmer, and skip all attempts to do anything
with the object (rather than generating confusing cascade error
messages). You definitely shouldn't expose the database connect
string in an error message to a user. It may also not be nice to
let a hacker know he took your database down.


<?php

require_once 'PDB.php';

try {
$db = PDB::connect('mysql:host=192.168.0.10;dbname=mydb',
'myname', 'secret');
$db->setFetchMode(PDO::FETCH_OBJ;
} catch (PDB_Exception $e) {
die("Could not connect: " . $e->getMessage() . "\n");
}

$sql = 'SELECT U.username FROM friends AS F JOIN users AS U ON
F.friendid = U.userid WHERE F.userid = ?';
$friends = $db->getCol($sql, 0, array((int)$_GET['userid']));
echo "Your friends are " . implode(', ', $friends);

?>

Or, can you use exceptions as warnings, or state notifications, with
guaranteed delivery?

If you generate an exception as a warning in a constructor, you get
guaranteed non-creation of the object. Is that what you want? Do
you use nuclear detonations as warnings?

In other words, I could have variables in my
object that a client programmer could use to understand what has
happened during instatiation, like:
if ( $myObj->state == "ok" ) {
... object using code goes here ...
}

but they may not think to check those variables. I might want to make
sure that the client programmer has to catch the exception, so I know
that they got the message. But, you can always say, it's up to the
client programming to know how to use the object.

I have an class that I want to instantiate in the middle of a page. If
it throws an exception, I really can't use it, so I don't need to
execute any of the follow-up code that interacts with the object. But
I also don't want to die in the middle of the page -- I have the whole
rest of the page to construct. So it seems I have to use some
inelegent code to do so:

<?php

... whole bunch of stuff ...

$use_object = TRUE;

try {
$myObj =& new objClass();

You could also put all of the stuff that uses the object HERE
and drop the use_object variable.

} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
$use_object = FALSE;
}

if ( $use_object ) {
... object code goes here ...
}

... rest of code ...

?>


.



Relevant Pages

  • Re: FxCop does anyone use it ?
    ... For instance it founds out if you catch Exception if you could have ... >the same time we developed our framework, so we find "new" warnings on old ... Internal MS code breaks many guidelines. ... We could never have complied to so many guidelines without FxCop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Catching generic exceptions
    ... Thanks for the input it is interesting to hear of other peoples experience with FxCop. ... >have to display the exception in a user friendly way. ... >exceptions in a other pieces of code that are non-GUI applications or do not ... This allows you to ignore the specific warnings only in the ...
    (microsoft.public.dotnet.languages.csharp)
  • Kernel Warning/crash using usb-serial device
    ... Currently I am testing USB serial device with BeagleBoardwhich ... uses musb host controller. ... But I gets following Warnings every time ... Exception stack ...
    (Linux-Kernel)
  • Exceptions - when and why?
    ... Now I'm wondering they why's and whens of exception throwing. ... the client programmer gets warnings popping up that they don't want. ... In a lot of examples I found, they die on exception. ...
    (php.general)
  • Exceptions - what do they mean?
    ... Now I'm wondering they why's and whens of exception throwing. ... the client programmer gets warnings popping up that they don't want. ... In a lot of examples I found, they die on exception. ...
    (comp.lang.php)