delphi - Are there exceptions that aren't caught by general E : Exception? -
i wondering if there exceptions/errors, make code jump except block aren't handled e : exception.
try := strtoint(s); {...do lot more...} except on e : econverterror begin showmessage('you need input valid number.'); end; on e : exception begin showmessage('something went wrong.'); raise; end; end;
is there way program have error ignore both statements in except block? or should :
try := strtoint(s); {...do lot more...} except on e : econverterror begin showmessage('you need input valid number.'); end; else begin // swapped on e : exception else showmessage('something went wrong.'); raise; end; end;
you can throw derives tobject
. in order catch every such class you'd need specify tobject
in on
statement.
from documentation:
exception types declared other classes. in fact, possible use instance of class exception, recommended exceptions derived sysutils.exception class defined in sysutils.
in reality know of no code throws not derive exception
, although @tlama points out 1 example in legacy deprecated vcl class in comments. strtoint
throws exception
descendents.
if don't need access exception object can use plain except
clause without on
statement.
try .... except // deal exceptions end;
or can use else
clause of on
statement catch all, again won't immediate access exception object.
otherwise can specify base class exceptions wish catch. instance on e: tobject
catches derived tobject
.
so, see, possible things not derived exception
may thrown. must ask if code ever that? if not makes sense test exception
in catch handlers. give access members of exception
. of course, 1 wonder why have catch exception handler. existence indication of poor design.
continuing theme of strtoint
, need catch econverterror
. that's raised when conversion fails. should ignore other exception class as, in example, code won't know else. 1 of goals of writing exception handling code handle know how deal with, , ignore else.
in fact, trystrtoint
need here:
if trystrtoint(s, i) // stuff else // deal conversion error
this obviates need handle exceptions , makes code far more readable.
i know strtoint
example, serves quite demonstrate benefits of trying avoid handling exceptions.
Comments
Post a Comment