Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out which exceptions a Delphi function might throw?

Is there a good way to find out which exceptions a procedure/function can raise in Delphi (including it's called procedures/functions)?

In Java you always have to declare which exceptions that can be thrown, but this is not the case in Delphi, which could lead to unhandled exceptions.

Are there any code analysis tools that detects unhandled exceptions?

like image 947
ajob Avatar asked Sep 16 '08 14:09

ajob


People also ask

What are the three types of exceptions?

There are three types of exception—the checked exception, the error and the runtime exception.

Why are exceptions thrown?

The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

What is the name of the exception related to database C++ Builder?

Delphi Exception Handling in C++ C++Builder libraries may throw exceptions when something unexpected occurs.


2 Answers

(Edit: It is now obvious that the question referred only to design-time checking.)

New answer:

I cannot state whether there are any tools to check this for you. Pascal Analyzer, for one, does not.

I can tell you, however, that in most Delphi applications, even if there was a tool to check this for you, you would get no results.

Why? Because the main message loop in TApplication.Run() wraps all HandleMessage() calls in an exception handling block, which catches all exception types. Thus you will have implicit/default exception handling around 99.999% of code in most applications. And in most applications, this exception handling will be around 100% of your own code - the 0.001% of code which is not wrapped in exception handling will be the automatically generated code.

If there was a tool available to check this for you, you would need to rewrite Application.run() such that it does not include exception handling.

(Previous answer: The Application.OnException event handler can be assigned to catch all exceptions that aren't handled by other exception handlers. Whilst this is run-time, and thus perhaps not exactly what you are after (it sounds like you want to identify them at design time), it does allow you to trap any exception not handled elsewhere. In conjunction with tools such as the JCLDebug stuff in the Jedi Code Library, you could log a stack trace to find out where & why an exception occurred, which would allow for further investigation and adding specific exception handling or prevention around the guilty code...)

like image 143
Graza Avatar answered Sep 21 '22 14:09

Graza


My guess is that you're trying to make Delphi behave like Java, which is not a good approach. I'd advise not to worry too much about unhandled exceptions. In the worst case, they'll bubble up to the generic VCL exception handler and cause a Windows message dialog. In a normal application, they won't halt the application.

Well-written code would document the different exceptions that can be raised so you can handle them in a meaningful way. Catch-all handlers aren't recommended since there is really no way to know what to do if you don't know why an exception was raised. I can also highly recommend madExcept.

like image 27
Frederik Slijkerman Avatar answered Sep 22 '22 14:09

Frederik Slijkerman