delphic++builderfiredac

How do you know what a method's exception codes are?


I typically haven't used exceptions in the past (I disable them), but with C++Builder there are things configured to use it. So I'll embrace it, but how do you know what the exceptions are, so you know what you want to do with it?

The old method of returning error codes or NULL or FALSE are fairly simple to know the code, because they typically document the code with the function, but I find that with exceptions there is really no information about them documented with the methods/commands.

For example, the TFDSQLiteSecurity.SetPassword() function, how do I know what any exception (return) codes are to respond to them? I don't see them listed?


Solution

  • Unless documented, the only way to know what type of exception is being thrown is to actually catch it at runtime and ask it for its type. Or look in the library source code, if available.

    The thing about exceptions is that you don't always need to know what they are, only to know that they are being thrown at all.

    Most exceptions are derived from common base classes (Sysutils::Exception, std::exception, etc), so it is usually enough to just catch an exception by its base class, not specific derived class. You need to know the specific type of an exception only if you want to treat specific exceptions differently than others, or if you need to access exception-specific details, such as for logging purposes.

    In any case, in your particular example, all FireDAC exceptions are derived from the EFDException class, which has an FDCode property holding a FireDAC error code. And DBMS-specific FireDAC exceptions are derived from EFDDBEngineException, which has several more properties describing database errors, including ErrorCode, the failed SQL and its Params, etc.

    Not all exceptions are documented on a per-method basis. Sometimes error documentation is more centralized. For instance, see the Handling Errors (FireDAC) documentation for more details about FireDAC exceptions.