473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

having "throw" write to a file before actually throwing


Hi!

<OT, background>
I am in a situation where I use two compilers from different
vendors to compile my program. It seems that recently, due to
a misconfiguration, library conflict or my ignorance, with one
of the compilers I am having trouble related to libuwind.so,
which, to my knowledge, deals with the intricacies of unwinding
the stack upon an exception. Executables compiled with this
compiler crash with a SEGV after throw(), never reaching catch(),
going through likes of _Unwind_ApplyVirtualRule just before they
die. Perhaps it's because throw()'s are located in a library
and are "caught()" in the main program.
</OT>

Now, for the C++ question...

Anyway, before I resolve this issue I'd like to obtain
a behaviour as-if "every throw statement wrote to a log file,
before actually throwing". Is there a kludge way to do it?

I tried an ugly trick of re#defining throw to something like
Assert(...),throw
but then the preprocessor replaces the word "throw" in
exception specifications too.

Trying to wrap all throws into some throw_ functions
seems like a lot of work. Perhaps there's an easy solution?

TIA,
- J.
Mar 3 '06 #1
13 2029
Jacek Dziedzic wrote:

Hi!

<OT, background>
I am in a situation where I use two compilers from different
vendors to compile my program. It seems that recently, due to
a misconfiguration, library conflict or my ignorance, with one
of the compilers I am having trouble related to libuwind.so,
which, to my knowledge, deals with the intricacies of unwinding
the stack upon an exception. Executables compiled with this
compiler crash with a SEGV after throw(), never reaching catch(),
going through likes of _Unwind_ApplyVirtualRule just before they
die. Perhaps it's because throw()'s are located in a library
and are "caught()" in the main program.
</OT>

Now, for the C++ question...

Anyway, before I resolve this issue I'd like to obtain
a behaviour as-if "every throw statement wrote to a log file,
before actually throwing". Is there a kludge way to do it?


You could let the exception object that you are about to throw do that in
its constructor.

Mar 3 '06 #2
Rolf Magnus wrote:
You could let the exception object that you are about to throw do that in
its constructor.


That's interesting, thanks!

- J.
Mar 3 '06 #3
Jacek Dziedzic wrote:
Rolf Magnus wrote:
You could let the exception object that you are about to throw do that in
its constructor.


That's interesting, thanks!


.... but then again, if I use __LINE__, __PRETTY_FUNCTION__
and things like that, they will refer to the constructor
of the root exception object, not the place of the throw.

What I really want is to have a log of where in my source
which exceptions occurred when.

thanks,
- J.
Mar 3 '06 #4
Jacek Dziedzic wrote:
[snip]
Anyway, before I resolve this issue I'd like to obtain
a behaviour as-if "every throw statement wrote to a log file,
before actually throwing". Is there a kludge way to do it?

I tried an ugly trick of re#defining throw to something like
Assert(...),throw
but then the preprocessor replaces the word "throw" in
exception specifications too.

Trying to wrap all throws into some throw_ functions
seems like a lot of work. Perhaps there's an easy solution?


Don't use exception specifications
(http://www.gotw.ca/publications/mill22.htm), and then your macro will
work. Alternately, use a macro for your actual throws, e.g.:

#define THROW( E, p ) \
{ \
E e_( p ); \
std::ofstream log_( "log.txt" ); \
if( log_ ) log_ << e_.what() << std::endl; \
throw e_; \
}

class MyException : public std::exception { ... };

THROW( MyException( "Some error text" ) );

Cheers! --M

Mar 3 '06 #5

Jacek Dziedzic wrote:
Hi!

<OT, background>
I am in a situation where I use two compilers from different
vendors to compile my program. It seems that recently, due to
a misconfiguration, library conflict or my ignorance, with one
of the compilers I am having trouble related to libuwind.so,
which, to my knowledge, deals with the intricacies of unwinding
the stack upon an exception. Executables compiled with this
compiler crash with a SEGV after throw(), never reaching catch(),
... Perhaps it's because throw()'s are located in a library
and are "caught()" in the main program.
</OT>
Not really OT. The exception mechanism is implementation-specific.
If you mix two compilers, you get Undefined Behavior like this. Don't
expect it to work.
Now, for the C++ question...

Anyway, before I resolve this issue I'd like to obtain
a behaviour as-if "every throw statement wrote to a log file,
before actually throwing". Is there a kludge way to do it?


No. Keep in mind that you could even have a throw (1); not to
mention a plain throw;

Furthermore, new throws as well. That's not a throw statement at all,
yet you's suffer the same effects.

HTH,
Michiel Salters

Mar 3 '06 #6

Jacek Dziedzic wrote:
I tried an ugly trick of re#defining throw to something like
Assert(...),throw
but then the preprocessor replaces the word "throw" in
exception specifications too.

Trying to wrap all throws into some throw_ functions
seems like a lot of work. Perhaps there's an easy solution?


You may be interested in the following approach
http://groups.google.com/group/comp....910d7a8dc49684
..

Mar 3 '06 #7
mlimber wrote:
Don't use exception specifications
(http://www.gotw.ca/publications/mill22.htm), and then your macro will
work.
I know not to use them, but system headers files do use them.
Alternately, use a macro for your actual throws, e.g.:

#define THROW( E, p ) \
{ \
E e_( p ); \
std::ofstream log_( "log.txt" ); \
if( log_ ) log_ << e_.what() << std::endl; \
throw e_; \
}

class MyException : public std::exception { ... };

THROW( MyException( "Some error text" ) );


Yes, but that's much work replacing the throws.

For now I have automatically replaced all instances
of "throw" with "logged throw" and did
#define logged \
std::cerr << __FILE__ << "\n" << __LINE__ << std::endl,

Works for now...

thanks,
- J.
Mar 3 '06 #8
Mi*************@tomtom.com wrote:
Jacek Dziedzic wrote:
Hi!

<OT, background>
I am in a situation where I use two compilers from different
vendors to compile my program. It seems that recently, due to
a misconfiguration, library conflict or my ignorance, with one
of the compilers I am having trouble related to libuwind.so,
which, to my knowledge, deals with the intricacies of unwinding
the stack upon an exception. Executables compiled with this
compiler crash with a SEGV after throw(), never reaching catch(),
... Perhaps it's because throw()'s are located in a library
and are "caught()" in the main program.
</OT>

Not really OT. The exception mechanism is implementation-specific.
If you mix two compilers, you get Undefined Behavior like this. Don't
expect it to work.


That I know. The problem is that my issue arises even when
I compile _both_ the library and the main program with the
_same_ compiler, with the _same_ options. I suspect that, because
the main program has parts in FORTRAN, it pulls some i/o
librariess that in turn pull a different version of the unwind
library than the one my library uses. But it's just a guess.

Now, for the C++ question...

Anyway, before I resolve this issue I'd like to obtain
a behaviour as-if "every throw statement wrote to a log file,
before actually throwing". Is there a kludge way to do it?

No. Keep in mind that you could even have a throw (1); not to
mention a plain throw;


Yes, I was rather interested in "ugly preprocessor tricks".
Furthermore, new throws as well. That's not a throw statement at all,
yet you's suffer the same effects.


Right. Yet I am only interested in the exceptions I throw
myself, so that wouldn't bother me.

thanks,
- J.
Mar 3 '06 #9
Jacek Dziedzic wrote:
mlimber wrote:
Don't use exception specifications
(http://www.gotw.ca/publications/mill22.htm), and then your macro will
work.


I know not to use them, but system headers files do use them.
Alternately, use a macro for your actual throws, e.g.:

#define THROW( E, p ) \
{ \
E e_( p ); \
std::ofstream log_( "log.txt" ); \
if( log_ ) log_ << e_.what() << std::endl; \
throw e_; \
}

class MyException : public std::exception { ... };

THROW( MyException( "Some error text" ) );


Yes, but that's much work replacing the throws.

For now I have automatically replaced all instances
of "throw" with "logged throw" and did
#define logged \
std::cerr << __FILE__ << "\n" << __LINE__ << std::endl,

Works for now...


Ok, but that is essentially the same solution I proposed and requires
the same amount of work to find/replace all the instances.

Cheers! --M

Mar 3 '06 #10
mlimber wrote:
Jacek Dziedzic wrote:
mlimber wrote:
Don't use exception specifications
(http://www.gotw.ca/publications/mill22.htm), and then your macro will
work.


I know not to use them, but system headers files do use them.

Alternately, use a macro for your actual throws, e.g.:

#define THROW( E, p ) \
{ \
E e_( p ); \
std::ofstream log_( "log.txt" ); \
if( log_ ) log_ << e_.what() << std::endl; \
throw e_; \
}

class MyException : public std::exception { ... };

THROW( MyException( "Some error text" ) );


Yes, but that's much work replacing the throws.

For now I have automatically replaced all instances
of "throw" with "logged throw" and did
#define logged \
std::cerr << __FILE__ << "\n" << __LINE__ << std::endl,

Works for now...

Ok, but that is essentially the same solution I proposed and requires
the same amount of work to find/replace all the instances.


Well, almost. With my method, since I don't use 'throw'
in any other context than for throwing exceptions, I could use
cat *.cpp *.h | sed "s/throw/logged throw/g"
to have it replaced.

Your method has the disadvantage of havind to add the ")"
after the exception name, which in many cases was not on the
same line as the "throw" keyword. That sounded a little tricky
for me to handle :).

thanks,
- J.
Mar 3 '06 #11
Jacek Dziedzic wrote:
mlimber wrote:
Jacek Dziedzic wrote:
mlimber wrote:

Don't use exception specifications
(http://www.gotw.ca/publications/mill22.htm), and then your macro will
work.

I know not to use them, but system headers files do use them.
Alternately, use a macro for your actual throws, e.g.:

#define THROW( E, p ) \
{ \
E e_( p ); \
std::ofstream log_( "log.txt" ); \
if( log_ ) log_ << e_.what() << std::endl; \
throw e_; \
}

class MyException : public std::exception { ... };

THROW( MyException( "Some error text" ) );

Yes, but that's much work replacing the throws.

For now I have automatically replaced all instances
of "throw" with "logged throw" and did
#define logged \
std::cerr << __FILE__ << "\n" << __LINE__ << std::endl,

Works for now...

Ok, but that is essentially the same solution I proposed and requires
the same amount of work to find/replace all the instances.


Well, almost. With my method, since I don't use 'throw'
in any other context than for throwing exceptions,


Now wait a minute. In the OP, you complained about the macro
invalidating exception specifications. Do you use them or not?
I could use
cat *.cpp *.h | sed "s/throw/logged throw/g"
to have it replaced.

Your method has the disadvantage of havind to add the ")"
after the exception name, which in many cases was not on the
same line as the "throw" keyword. That sounded a little tricky
for me to handle :).


That's just mechanics. You could use regular expressions (or some other
utility) to do the more complex substitutions. My point remains that
you must do the same number of substitutions.

Cheers! --M

Mar 3 '06 #12
ma***********@gmail.com napisał(a):

Ok, but that is essentially the same solution I proposed and requires
the same amount of work to find/replace all the instances.
Well, almost. With my method, since I don't use 'throw'
in any other context than for throwing exceptions,



Now wait a minute. In the OP, you complained about the macro
invalidating exception specifications. Do you use them or not?


As I said -- I don't use them, but the system/compiler
header files do. Like when I redefined "throw" I got a
lot of errors from .h files that weren't mine and the
errors pointed to exception specifications.
I could use
cat *.cpp *.h | sed "s/throw/logged throw/g"
to have it replaced.
In fact all my exception class names begin with 'E',
so it was even easier -- I have replaced "throw E".

Your method has the disadvantage of havind to add the ")"
after the exception name, which in many cases was not on the
same line as the "throw" keyword. That sounded a little tricky
for me to handle :).



That's just mechanics. You could use regular expressions (or some other
utility) to do the more complex substitutions. My point remains that
you must do the same number of substitutions.


Yes, I agree. Let's say it has the same time complexity, albeit
with a bigger prefactor, because I don't know how to use regexps
properly :). Plus your method is cleaner and mine is not even
guaranteed to work, afair, because it redefines a keyword.

- J.
Mar 3 '06 #13

Jacek Dziedzic wrote in message
<2a***************************@news.chello.pl>.. .
Jacek Dziedzic wrote:
Rolf Magnus wrote:
You could let the exception object that you are about to throw do that in
its constructor.


That's interesting, thanks!


... but then again, if I use __LINE__, __PRETTY_FUNCTION__
and things like that, they will refer to the constructor
of the root exception object, not the place of the throw.

What I really want is to have a log of where in my source
which exceptions occurred when.
thanks,
- J.


Maybe this will give you an idea.
// --- std includes ---
#include <iostream> // C++
#include <ostream> // std::endl
#include <string>
// #include <sstream> // for __LINE__ conversion to string
#include <stdexcept>

class MyError : public std::runtime_error { public:
MyError(std::string const &msg = "") : std::runtime_error(msg){}
};
// --------------------------------------

void f() throw( MyError ){
std::string Msg( __FILE__": my message"
"\n Thrown from deep within ");
Msg += __PRETTY_FUNCTION__; // non-portable (GCC)
throw MyError( Msg );
// -- or (more portable):
// throw MyError(__FILE__": my message"
// "\n Thrown from deep within f()" );
return;
}

int main(){
try{ f(); }
catch( MyError const &x){
std::cout<<"MyError: "<<x.what()<<std::endl;
// throw; // re-throw, kill the program
}
return 0;
} // main()end

--
Bob R
POVrookie
Mar 3 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Cheng Mo | last post by:
Below code cannot be compiled. try { throw std::string; } catch(...) { } While below is correct.
1
by: qazmlp | last post by:
I have a class like this: class base { public: virtual ~base() throw(); // Other members }; Now, if I write a class like this:
15
by: bill salkin | last post by:
I'd like to create a custom error handler like this in VB.NET: .... try ... Throw ("Lender Name not in table") .... catch ("Lender Name not in table")
3
by: Dan | last post by:
Hi all! When I throw my custom Exception class the first time in my code, the compiler takes a lot of time for find the following catch EX: try Throw New MyCustomException("test")
1
by: Jim Davis | last post by:
I've been happily using a custom error handler of the following form for a while now: window.onerror = function(Message, URL, Line) { ... } The current case is an intranet application (support...
28
by: kaferro | last post by:
What is the safest way to make an argv comparison? The code below works. #include <iostream> #include <string> using namespace std; int main(int argc, char *argv) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.