473,249 Members | 1,747 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,249 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 2023
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) {
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

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.