Root > How to... > ...handle an exception? > ...handle a C++ exception?

...handle a C++ exception?

Previous pageReturn to chapter overviewNext page   

EurekaLog automatically catches any unhandled exceptions in your application. However, you may want to force-handle exception coming from a specific code:

 

try
{
  // ... some C++ code here ...
}
catch (...)
{
  // How to handle this?
}

 

EurekaLog is a Delphi library. As such - it does not have direct access to C++ code. It means that C++ standard exceptions, as well as user-defined C++ exceptions, cannot be handled by the Delphi run time. Therefore Delphi's ExceptObject and ExceptAddr functions will return nil (NULL). Hence the EurekaLog will also see no exception.

 

If C++ Exceptions are not explicitly caught (in other words: there is no try-catch block), they are eventually caught by Delphi exception handlers (RTL or VCL code). When the Delphi RTL encounters an exception that is not Delphi generated or hardware related, the exception is mapped as an EExternalException. So, if a C++ exception is not explicitly caught and it leaks into the Delphi run time, the application raises an External Exception EEFFACE. Once that happens, EurekaLog steps in and handles this External Exception EEFFACE as a standard Delphi exception. For example:

 

try
  // ... some C++ code here ...
except // - a Delphi exception-wrapper (EExternalException) is created here
  on E: Exception do 

    // E is the EExternalException (a Delphi exception) with the 0x0EEFFACE error code
end;

 

If this is the case - you can follow the How to handle an exception guide.

 

In any other cases when:

1. Exception is a C++ exception (not a Delphi exception);
2. Exception is catched by the C++ catch block (not Delphi except block);
3. You need EurekaLog to process this exception (handling by your code is not enough)

you should wrap your C++ code in a Delphi code. For example, you can create the following Delphi unit:

 

unit DelphiTools;
 
interface
 
type
  TCppFunction = procedurecdecl;           // a C++ function
  TCppMethod   = procedure of objectcdecl// a C++ method
 
procedure ExecuteCppCode(const ACppCode: TCppFunction); overload;
procedure ExecuteCppCode(const ACppCode: TCppMethod); overload;
 
implementation
 
uses
  System.Classes;
 
procedure ExecuteCppCode(const ACppCode: TCppFunction);
begin
  try
    ACppCode;
  except
    System.Classes.ApplicationHandleException(nil); 
  end;
end;
 
procedure ExecuteCppCode(const ACppCode: TCppMethod);
begin
  try
    ACppCode;
  except
    System.Classes.ApplicationHandleException(nil); 
  end;
end;
 
end.

 

The main idea here is to call your C++ code from a Delphi code, so you can use Delphi's try-except block.

 

This code is just an example. You can adjust it or write your own code. Specifically, you can replace the content of the except block. We just call a default RTL exception handler in the above example, but you may add any custom processing that you want.

 

Once you have such code, you can do the following:

 

[BEFORE] (old code, which should be replaced)

 

try
{
  // ... some C++ code here ...
}
catch (...)
{
  // How to handle this?
}
 

 
[AFTER] (new code, which you should use)

 
void ASampleFunc()
{
  // ... some C++ code here ...
}
 
DelphiTools::ExecuteCppCode(ASampleFunc);

 

 

This code will run your C++ code from the ASampleFunc function inside a Delphi try-except block. Therefore, any C++ exceptions thrown by your code will be wrapped by Delphi RTL into a Delphi exception object (EExternalException) - which can be catched and handled by EurekaLog.

 

 

See also:




Send feedback... Build date: 2024-12-19
Last edited: 2024-07-08
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/how_to_handle_cpp_exception.php