Are there exceptions that are not caught by the generic E: Exception?

I was wondering if there are exceptions / errors that make your code jump into an exception block but are not handled by E: exceptions.

 try
   i := StrToInt(s);
   {...do a lot more...}
 except
   on E : EConvertError do begin
     ShowMessage('You need to input a valid number.');
   end;
   on E : Exception do begin
     ShowMessage('Something went wrong.');
     Raise; 
   end;
 end;

      

Is there a way the program could have had an error that would ignore both statements in this exclusive block? Or should I do it like this:

 try
   i := StrToInt(s);
   {...do a lot more...}
 except
   on E : EConvertError do begin
     ShowMessage('You need to input a valid number.');
   end;
   else begin  // swapped on e : Exception with else
     ShowMessage('Something went wrong.');
     Raise; 
   end;
 end;

      

+3


source to share


1 answer


You can throw away everything that comes from TObject

. To catch each such class, you need to specify TObject

in the instructions on

.

In the documentation:

Exception types are declared in the same way as other classes. In fact, an instance of any class can be used as an exception, but it is recommended that exceptions be thrown from the SysUtils.Exception class defined in SysUtils.

Actually I don't know any code that throws anything that doesn't come from Exception

, although @TLama points out one example in a deprecated legacy VCL class in the comments. Of course, it StrToInt

only throws out Exception

children.

If you don't need access to the exception object, you can use a simple clause except

without instructions on

.

try
  ....
except 
  // deal with all exceptions
end;

      

Or, you can use the else

statement clause on

as catch all and you won't get immediate access to the exception object.



Otherwise, you can specify a base class for the exceptions you want to catch. For example, it on E: TObject

catches everything received from TObject

.

So, as we can see, it is possible that things not received from Exception

can be thrown away. But you have to ask yourself, does your code really do this? If not, then it makes sense to test Exception

all handlers in your catch. This will give you access to the members Exception

. Of course you are wondering why you have a catch exception handler. Their existence is often indicative of poor design.

Continuing the topic StrToInt

, you only need to catch EConvertError

. This is what happens when the conversion fails. You should ignore any other class of exceptions, since in your example the code won't know what to do with anything else. One of the goals of writing exception handling code is to handle what you know how to deal with and ignore everything else.

In fact, TryStrToInt

this is what you need here:

if TryStrToInt(s, i) then
  // do stuff with i
else
  // deal with conversion error

      

This removes the need to handle any exceptions and makes your code more readable.

I know this StrToInt

is just an example, but it does a pretty good job of demonstrating the benefits of trying to avoid exception handling.

+7


source







All Articles