Root > How to... > ...handle an exception?

...handle an exception?

Previous pageReturn to chapter overviewNext page   

Important note for C++ Builder users: see How to handle a C++ exception?

 

Important: as a general rule, you should write your application in a such way that it will behave correctly without EurekaLog on board. This means that if your or 3rd party code throws an exception - it must be handled in a way that you expect. For example, by showing an error message, canceling action, retrying, etc. Once your application behaves like it is supposed to do - then you add EurekaLog for it. EurekaLog will auto-handle all unhandled exceptions automatically (by hooking few well-known places), and for everything else - you can call EurekaLog from your own handlers when needed. In other words:

Incorrect: your application does not show messages for exceptions (or do other stuff that you want) and you are adding EurekaLog in hopes to fix this behavior;
Correct: your application correctly handles exceptions and you are adding EurekaLog to receive reports about such exceptions.

 

 

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

 

try
  // ... some code ...
except
  on E: ENetworkError do
  begin
    // How to handle this?
  end;
end;

 

If this is the case - then you can use any of the following:

1. [VCL] Application.HandleException(nil); from Forms unit
2. [VCL] Application.ShowException(E); from Forms unit
3. [RTL] ApplicationHandleException(nil); from Classes unit
4. [RTL] ApplicationShowException(E); from Classes unit
5. [EurekaLog] HandleException(E); from EBase unit
6. [EurekaLog] ShowExpected(E); from EBase unit
7. [EurekaLog] Exception2HRESULT(E); from EAppDLL unit

 

For example:

 

try
  // ... some code ...
except
  on E: ENetworkError do
    Application.HandleException(nil);
end;

 

or:

 

try
  // ... some code ...
except
  on E: ENetworkError do
    ApplicationShowException(E);
end;

 

This code will show a usual error message box when EurekaLog is not enabled/active, but it will show full EurekaLog's dialog (with call stack, sending, etc.) when EurekaLog is enabled and active.

 

Notes:

Use item 6 (ShowExpected) only if you want to consider current exception as "expected".
Use last example (Exception2HRESULT) only when you need to convert exception to HRESULT value to pass it between executable modules. E.g. when you are developing a DLL. See this article for an example.

 

Important Notes:

If you are going to handle exception from another thread (for example, when you are analyzing .FatalException property of a background thread in the main thread) - be sure to use a function which allows you to specify exception object directly. For example, Application.HandleException function will try to use current exception, it does not know about your thread and its .FatalException property. It will not work properly for your purpose. On the other hand, Application.ShowException allows you to specify an exception object as the first argument. Thus, you may pass your MyThread.FatalException to it.
Some RTL and VCL handlers will call EurekaLog only when low-level injection hooks are enabled (which is a default). If you disable low-level hooks - be sure to use EurekaLog's functions to handle exceptions.
If your exception was not raised yet - then EurekaLog will ignore it. If you want to force EurekaLog to handle such exception - see this article.

 

 

See also:




Send feedback... Build date: 2024-09-30
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_exception.php