Delphi – Is there a general E-unfailed exception: exception?

I want to know if there is an exception/error, which will cause your code to jump to an except block but will not be processed by the E: exception.

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 to make a program erroneously ignore the two statements other than the block?
Or should I do 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;

You can throw anything derived from TObject. In order to capture each such class, you need to specify TObject in the on statement.

< /p>

From the documentation:

Exception types are declared just like other classes. In fact, it is possible to use an instance of any class as an exception, but it is recommended that exceptions be derived from the SysUtils.Exception class defined in SysUtils.

Actually, I know that no code can throw anything that is not derived from Exception, although @TLama pointed out an example in the old deprecated VCL class in the comments. Of course StrToInt will only throw Exception descendants.

If you don’t need to access the exception object, you can use the plain except clause Instead of using the on statement.

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

Or you can use the else clause of the on statement as catch all, again you will not access the exception object immediately.

Otherwise , You can specify the base class for the exception to be caught. For example, E: TObject captures everything derived from TObject.

So, as we have seen, it may throw something that is not derived from Exception Stuff. But you have to ask yourself, has your code ever done this? If not, then it makes the most sense to test the Exception in a catch catch all handler. This will give you access to the members of the Exception. Of course, people do want to know why you have a catch all exception handler. Their presence often indicates the design Poor.

Continuing the topic of StrToInt, you only need to catch EConvertError. This is the problem that is raised when the conversion fails. You should ignore any other exception classes, because in your example, the code will not know how Handle any other exceptions. One of the goals of writing exception handling code is to handle the content you know how to handle, and ignore everything else.

In fact, TryStrToInt is what you need:

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

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

I know StrToInt is just an example, but it is a good demonstration of the benefits of trying to avoid exceptions.

I want to know if there are exceptions/errors, which will cause your code to jump to an except block but will not be processed by the E: exception.

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 to make a program erroneously ignore the two statements other than the block?
Or should I do 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;

< /p>

You can throw anything derived from TObject. In order to capture each such class, you need to specify TObject in the on statement.

From the documentation:

Exception types are declared just like other classes. In fact, it is possible to use an instance of any class as an exception, but it is recommended that exceptions be derived from the SysUtils.Exception class defined in SysUtils.

Actually, I know that there is no code that can throw anything that is not derived from Exception, although @TLama pointed out in the comments that the old version is deprecated An example in the VCL class. Of course StrToInt will only throw Exception descendants.

If you don’t need to access the exception object, you can use the plain except clause instead of the on statement.

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

Or you can use on Els of the statement The e clause serves as catch all, and again you will not access the exception object immediately.

Otherwise, you can specify the base class for the exception to be caught. For example, E: TObject captures all content derived from TObject.< /p>

So, as we have seen, something that is not derived from Exception may be thrown. But you have to ask yourself, has your code ever done this? If not, then it makes the most sense to test the Exception in a catch catch all handler. This will give you access to the members of the Exception. Of course, people do want to know why you have a catch all exception handler. Their presence often indicates the design Poor.

Continuing the topic of StrToInt, you only need to catch EConvertError. This is the problem that is raised when the conversion fails. You should ignore any other exception classes, because in your example, the code will not know how Handle any other exceptions. One of the goals of writing exception handling code is to handle the content you know how to handle, and ignore everything else.

In fact, TryStrToInt is what you need:

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

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

I know StrToInt is just an example, but it is a good demonstration of the benefits of trying to avoid exceptions.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3144 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.