Mailinglist Archive: yast-devel (81 mails)

< Previous Next >
Re: [yast-devel] About catching exceptions
  • From: Josef Reidinger <jreidinger@xxxxxxx>
  • Date: Thu, 22 Apr 2010 12:19:06 +0200
  • Message-id: <201004221219.07208.jreidinger@xxxxxxx>
In general in exception handling is basic rule, handle only exception which you
know and know how to avoid or inform user better then general exception handler
know it. So calling function which might raise exception is common thing and it
is handled on place where you have enough information about effect of
exceptions so instead of immediate catch of IO.open exception you can catch in
e.g. function which call load_configuration. Then you know that you don't have
configuration due to some well known problems (so never catch all exception
unless you must unrelevant problems e.g. in code). So you catch IO exception
when you call load configuration handle it (use default or print to users) and
you still can log information about exception.

So to simplify it to simple rules:
- always catch only limited exception, generic one is caught by global
exception catcher.
- catch when you know effects of problem so you can qualify handle it (like
load default etc.)
- never catch exception only to log problem ( sometime I see it in webyast, it
is horrible as it just duplicates code and generic handler log it), if you do
not want to ignore it.
- always log exception if you do not rethrow it

So for Klaus examples:
1) it is all functions as each function raise exception, so only if you know
exception and can handle in expected way
2) Good one if you in catch handle it
3) it is not DRY. It duplicates function of global exception catch point and
also in some case it is error as it can hide problems and program can start
behave strange without any visible reason. (like current System, which just
write system cannot boot without anything. It just confuse and hides potential
problem.)

Josef

Martin Vidner write:
On Thu, Apr 22, 2010 at 11:36:58AM +0200, Klaus Kaempf wrote:
when calling functions known to raise exceptions, its always good
practice to enclose calls with begin ... rescue ... end and catch
exceptions locally.

Good that you bring it up because I think that you got it absolutely
wrong.

Unless you give more specific examples, this only creates more
problems by bypassing the generic, well-thought out system-wide
recue from.

Your code will hide error details, or cause unnecessary code
duplication.

Catching exceptions should cover known problems (i.e. missing
permissions) as well as generic problems.

Example:


WRONG:

call_function_which_might_raise

CORRECT, unknown exceptions are handled by exception_trap in
http://gitorious.org/opensuse/yast-web-client/blobs/master/webclient/app/controllers/application_controller.rb

NOT ENOUGH:

begin
call_function_which_might_raise
rescue KnownException => e
handle_known_exception e
end

debatable

GOOD:

begin
call_function_which_might_raise
rescue KnownException => e
handle_known_exception e
rescue Exception => e
handle_generic_exception e
end

WRONG, as I said first

The 'wrong' and 'not enough' cases validate bug reports.

I don't understand what you mean by this. Bug reports are validated
by the developer reproducing them.


--
Josef Reidinger
YaST team
maintainer of perl-Bootloader, YaST2-Repair, parts of webyast
--
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-devel+help@xxxxxxxxxxxx

< Previous Next >