Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Outputting detailed exception errors



Permlink Replies: 3 - Last Post: Jun 28, 2017 2:46 PM Last Post By: Rudy Velthuis (...
Andy Stobirski

Posts: 18
Registered: 6/12/16
Outputting detailed exception errors
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 27, 2017 3:09 AM
Hi All

I'm working in C++ Builder v5.0 sp1, and want to output more information on exceptions. So far, I have the following:

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Application->OnException = AppException;
}
 
void __fastcall TForm1::AppException(TObject * Sender, Sysutils::Exception * E)
{
    Application->ShowException(E);
}


And if I try to do something like call a method of an null object I'll get an error like:

Access violation at address 00401A0B in module 'Project2.exe'. Read of address 00000000.


Which isn't particularly helpful to me, so I want to add more information. Can I do things like the output the entire StackTrace (to a local file for example), or the method name the exception occured within etc?

Cheers

Andy
Jan Dijkstra

Posts: 206
Registered: 11/4/99
Re: Outputting detailed exception errors
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 27, 2017 11:56 PM   in response to: Andy Stobirski in response to: Andy Stobirski
Andy Stobirski wrote:
Hi All

I'm working in C++ Builder v5.0 sp1, and want to output more information on exceptions. So far, I have the following:

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Application->OnException = AppException;
}
 
void __fastcall TForm1::AppException(TObject * Sender, Sysutils::Exception * E)
{
    Application->ShowException(E);
}


And if I try to do something like call a method of an null object I'll get an error like:

Access violation at address 00401A0B in module 'Project2.exe'. Read of address 00000000.


Which isn't particularly helpful to me, so I want to add more information. Can I do things like the output the entire StackTrace (to a local file for example), or the method name the exception occured within etc?

Cheers

Andy

Yes, but it's not that easy. You need to access the helper API from Windows to access that kind of information, and write a routine to create the output you want. I've done it in our base library package, including hooking it into the vectored exception handling routine API (so it works independently of any try catch frame).
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Outputting detailed exception errors
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 1:31 PM   in response to: Andy Stobirski in response to: Andy Stobirski
Andy Stobirski wrote:

Can I do things like the output the entire StackTrace (to a local file
for example), or the method name the exception occured within etc?

Yes, but not easily. In modern versions (but not in BCB5), the
Sysutils::Exception class has a public StackTrace property, but it is
not hooked up by default (why Borland/Embarcadero do not hook it up is
beyond understanding, considering they are the best people to do so).
You have to install a 3rd party exception logger to hook up stack
tracing, such as MadExcept, EurekaLog, etc.

There is no native functionality in the RTL to determine method names
from memory addresses, but exception loggers usually expose their own
functionality for that (since they are the ones walking the stack and
logging method names for each stack frame when possible). That
requires compiling the project with MAP file generation enabled in the
project options. That will produce a .map file that contains the
relative addresses of every unit, function and class method in the
project. At runtime, those addresses are relative to the base memory
address that the app starts running from (usually 0x00401000, but not
always). You usually have to deploy that .map file with your app, or
link it into the app's resources during compiling, so the exception
logger can use it at runtime.

Just know that resolving an error memory address to a given function is
usually not as helpful as you think. It won't give you the exact line
of code that failed, and if the failure occured in a system function
then it won't tell you what function in your own code was calling it.
You need the full stack trace to get that kind of information. So,
just do yourself a failure and get a good exception logger that
supports BCB5, don't try to handle this manually.

--
Remy Lebeau (TeamB)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Outputting detailed exception errors
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 2:46 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

(why Borland/Embarcadero do not hook it up is
beyond understanding, considering they are the best people to do so).

Probably because they have other, more important, things to do...

In the meantime, some third parties seem to have been doing that.

--
Rudy Velthuis http://www.rvelthuis.de

"Intellectuals solve problems; geniuses prevent them."
-- Albert Einstein
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02