How to implement exceptions in ROOT?

Hello,

I am trying to run the following macro:

[code]#include
#include
#include
using namespace std;

class myexc : public exception
{
public:
myexc (const char* w) throw ()
{
_what = w;
}

virtual ~myexc () throw ()
{
}

virtual const char* what () const throw()
{
return _what.c_str ();
}

private:
string _what;
};

void f ()
{
cout << “hello” << endl;
throw myexc(“bye bye”);
cout << “coucou” << endl;
}

void except ()
{
f ();
}
[/code]

I run this in the following way:

And get the following error:

Processing except.C+g...
Info in <TUnixSystem>: creating shared library /home/giraudpf/phys/atlas/Asap2/macro/./except_C.so
/home/giraudpf/phys/atlas/Asap2/macro/./filenAN2jr.cxx:39: erreur: looser throw specifier for ‘virtual ROOT::Shadow::myexc::~myexc()’
/usr/include/c++/4.0.2/exception:58: erreur:   overriding ‘virtual std::exception::~exception() throw ()’
g++: /home/giraudpf/phys/atlas/Asap2/macro/./filenAN2jr.o : Aucun fichier ou répertoire de ce type
Error in <ACLiC>: Compilation failed!

Do you know what is wrong? What is the correct way to implement an exception in ROOT?

Thanks

Pierre-François

Hi Pierre-François,
what root version do you use? I believe this was fixed at some point… Could you try with the latest dev release?
Axel.

Hi Axel,

I am using ROOT version

* Version 5.08/00 13 December 2005 *

Is that early enough?

I can switch version, but I would much better like to stick to this version for the moment (several people are using my code) Do you know if there is a way to workaround this problem?

Pierre-François

Hi,
hmm, I thought I fixed it, but I didn’t :-/ I assume you don’t need a dictionary for your class - you don’t want to store an exception to file, and you don’t want to allow users to throw it from a script. Then you should be able to surround this class by “#ifndef CINT”. I’ll fix this issue tomorrow, but that won’t help you with v5.08.
Cheers, Axel.

Hi Axel,

I have tried surrounding the class with #ifndef CINT, but then I get the following error:

[code]Processing except.C+g…
Info in : creating shared library /home/giraudpf/phys/atlas/Asap2/macro/./except_C.so
hello
Error: G__getvariable: expression :0:
Error: Can not access pointer to function 0x0 from interpreter(1) FILE: LINE:0
Error: $(*)158443920 Syntax error? :0:
Syntax Error: * :0:
Error: Illegal pointer operation (tovalue) :0:

*** Break *** segmentation violation
Generating stack trace…
0xb760d9e9 in G__new_operator + 0x1ef from /usr/lib/root/libCint.so.5.08
0xb75d1899 in G__getexpr + 0x5df from /usr/lib/root/libCint.so.5.08
0xb7563171 in G__ExceptionWrapper + 0x463 from /usr/lib/root/libCint.so.5.08
0xb760fc40 in G__call_cppfunc + 0x2cc from /usr/lib/root/libCint.so.5.08
0xb75fa330 in G__interpret_func + 0x7e0 from /usr/lib/root/libCint.so.5.08
0xb75f0598 in G__getfunction + 0x1e4c from /usr/lib/root/libCint.so.5.08
0xb75d0b2b in G__getitem + 0x5eb from /usr/lib/root/libCint.so.5.08
0xb75db592 in G__getexpr + 0xa2d8 from /usr/lib/root/libCint.so.5.08
0xb75dd2fd in G__calc_internal + 0x299 from /usr/lib/root/libCint.so.5.08
0xb7638102 in G__process_cmd + 0x23d0 from /usr/lib/root/libCint.so.5.08
0xb798dace in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0x126 from /usr/lib/root/libCore.so.5.08
0xb798b29f in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) + 0x45 from /usr/lib/root/libCore.so.5.08
0xb78ca358 in TApplication::ProcessFile(char const*, int*) + 0x608 from /usr/lib/root/libCore.so.5.08
0xb78c77d4 in TApplication::ProcessLine(char const*, bool, int*) + 0x5c2 from /usr/lib/root/libCore.so.5.08
0xb6be766d in TRint::Run(bool) + 0x1e9 from /usr/lib/root/libRint.so.5.08
0x08048e60 in main + 0x68 from /usr/bin/root.exe
0xb6995ec2 in __libc_start_main + 0xd2 from /lib/tls/i686/cmov/libc.so.6
0x08048d61 in TApplicationImp::ShowMembers(TMemberInspector&, char*) + 0x31 from /usr/bin/root.exe
[/code]

If I look at the code in cint/src/Api.cxx line 378 (in version 5.08 ), I was expecting to get instead a nicer error message from cint! (bug or bad usage on my part?)

I will try your new version if you post a fix.

Pierre-François

Hi,
this is a different issue - for some reason, Cint fails to catch (or handle) your exception. I’ll check tomorrow, too. But at least (as you can see from the “hello”) you managed to build the library, and you managed to start it.
Axel.

Hi,

part 1 (d’tor looser throw) has been fixed in cint’s CVS, and will be migrated into ROOT’s cvs at some point. A work-around for part 2 of the problem (unknown G__exception) is to do call gSystem->Load("$(ROOTSYS)/cint/stl/exception") before you throw an exception (e.g. before you load your macro). I’ll try to get auto-loading enabled for G__exception, so Cint knows what lib to load when it needs it.

Cheers, Axel.