Search code examples
delphiexception

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 can be thrown, but this is not the case in the Delphi programming language, which could lead to unhandled exceptions.

Are there any code analysis tools that detect unhandled exceptions?


Solution

  • (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...)