The Right Approach to Exceptions

deadalnix deadalnix at gmail.com
Sat Feb 18 15:30:33 PST 2012


Le 18/02/2012 22:14, Andrei Alexandrescu a écrit :
> On 2/18/12 1:27 PM, Alex Rønne Petersen wrote:
>> Point taken. I suggested GetOptException initially because, based on
>> usage patterns of std.getopt, it seems to be most common to just catch
>> the exception (be it a parsing error, type coercion error, or whatever)
>> and print it.
>
> In fact for the vast majority of exceptions it seems to be most common
> to just catch the exception and print it.
>
> Tutorial examples never help, either. They go something like:
>
> try {
> ... stuff ...
> } catch (DatabaseException e) {
> print("There was a database exception: ", e);
> } catch (NetworkException e) {
> print("There was a network exception: ", e);
> }
>
> etc.
>
> Andrei

This is a dumb example. This show more the fact that Java forcing you to 
catch every single expression is a bad idea than the fact that having 
several types of exception is bad.

If you have nothing to do with that exception and don't know how to 
recover from it, just don't catch it. Or catch them all (Pokemon !!!) 
using catch(Exception e).

You know that « real life » programming is different than a tutorial.


More information about the Digitalmars-d mailing list