Visual C++ Exceptions

J

JCav

It's been a while since I've used Visual C++ and I'm getting an exception
from an unknown source. I'm writing Java JNI code, calling Java from C++. Is
there any way to get more information about what's getting thrown? I've
tried to catch it with a variety of catch clauses with all sorts of types.
The only catch that works is catch(...). My catches look like:

catch(const string &msg) {
cerr << msg.data() << endl;
}
catch(const char * msg) {
cerr << msg << endl;
}
catch(CException msg) {
cerr << msg.ReportError() << endl;
}
catch(CException *msg) {
cerr << msg->ReportError() << endl;
}
catch(int msg) {
cerr << msg << endl;
}
catch(const runtime_error &err) {
cerr << err.what() << endl;
}
catch(const logic_error &err) {
cerr << err.what() << endl;
}
catch(const bad_exception &err) {
cerr << err.what() << endl;
}
catch(const exception &err) {
cerr << err.what() << endl;
}
catch(...) {
cerr << "unknown exception" << endl;
}
 
A

Alf P. Steinbach

* JCav:
It's been a while since I've used Visual C++ and I'm getting an exception
from an unknown source. I'm writing Java JNI code, calling Java from C++.

Visual C++ is off-topic here, largely. JNI is off-topic here, largely.
Except where there is some general standard C++ issue.

Please post to an appropriate group.

Since you're doing JNI, most probably your exception source is JNI, and
thus a Java programming group would be indicated (also, that library is
probably where you'll have to look for more exception information).
 
J

Jim Langston

Alf P. Steinbach said:
* JCav:

Visual C++ is off-topic here, largely. JNI is off-topic here, largely.
Except where there is some general standard C++ issue.

Please post to an appropriate group.

Since you're doing JNI, most probably your exception source is JNI, and
thus a Java programming group would be indicated (also, that library is
probably where you'll have to look for more exception information).

Actually, I think his actual question is OT here, I just don't have the
answer. The question being, if something is being thrown, how do you find
out what the type is of what's being thrown without knowing it before hand?

My only suggestion would be to throw it in a debugger and run it and see
where it's throwing.
 
G

GB

JCav said:
It's been a while since I've used Visual C++ and I'm getting an exception
from an unknown source. I'm writing Java JNI code, calling Java from C++. Is
there any way to get more information about what's getting thrown? I've
tried to catch it with a variety of catch clauses with all sorts of types.
The only catch that works is catch(...). My catches look like:

catch(const string &msg) {
cerr << msg.data() << endl;

You can just say

cerr << msg << endl;

The << operator is overloaded to handle string objects directly.
}
catch(const char * msg) {
cerr << msg << endl;
}
catch(CException msg) {
cerr << msg.ReportError() << endl;
}
catch(CException *msg) {
cerr << msg->ReportError() << endl;
}
catch(int msg) {
cerr << msg << endl;
}
catch(const runtime_error &err) {
cerr << err.what() << endl;
}
catch(const logic_error &err) {
cerr << err.what() << endl;
}
catch(const bad_exception &err) {
cerr << err.what() << endl;
}

The three exceptions above are all derived from the one you are catching
below. Since you are handling all of them the same way (printing the
result of what()), it is not necessary to catch them.
catch(const exception &err) {
cerr << err.what() << endl;
}
catch(...) {
cerr << "unknown exception" << endl;
}

This is non-standard, so off-topic here, but Visual C++ by default
arranges to throw a special C++ exception when certain illegal
operations, such as dereferencing a null pointer, are performed. This
exception is not derived from any accessible exception type, so you
can't catch it except by "..." as you did. To determine if this is what
is happening, you need to run your software under a debugger. I
recommend setting the appropriate compiler options to disable this
behavior. Then when such an operation happens, you will crash instead,
and you can elect to attach with a JIT debugger.

Gregg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top