Exception information in catch(...) statement

B

Bernie

Hi

is it possible to receive information like type of exception (division by
zero/null pointer/...), memory adress or source line number in a catch(...)
statement?

main()
{
try
{
// do something
} catch(...)
{
// get more information about the exception
}
}

Thanks.
Bernie
 
Z

Zeppe

Bernie said:
Hi

is it possible to receive information like type of exception (division by
zero/null pointer/...), memory adress or source line number in a catch(...)
statement?

no, you can't. In order to have information on a specific type of
exception, you have to catch it explicitely with catch(ExceptionClass&
exc), for source line number the only way to get it is through a macro,
and I'm not aware of any mechanism to retrieve memory address information.

Best wishes,

Zeppe
 
M

mlimber

no, you can't. In order to have information on a specific type of
exception, you have to catch it explicitely with catch(ExceptionClass&
exc), for source line number the only way to get it is through a macro,
and I'm not aware of any mechanism to retrieve memory address information..

Right. You must encode any information you want to retrieve in the
exception at the throw point. That can include filename/line number
and anything else. If you've caught by "...", you can rethrow and try
to catch more specifically:

class MyException : public std::exception
{
const char* file_;
const int line_;
const char* desc_;
public:
MyException( const char* file, const int line, const char* desc )
: file_( file ), line_( line ), desc_( desc )
{}

const char* GetFile() const { return file_; }
int GetLine() const { return line_; }
const char* GetDesc() const { return desc_; }

virtual const char* what() const { return desc_; }
};

// ...
try
{
// ...
throw MyException( __FILE__, __LINE__, "Something bad happened" );
}
catch( ... )
{
try
{
throw; // rethrow
}
catch( const MyException& e )
{
std::cerr << "Exception at " << e.GetFile() << ':'
<< e.GetLine() << ':' << e.GetDesc() << '\n';
}
catch( const std::bad_alloc& )
{
std::cerr << "Memory allocation error.\n"
}
catch( const std::exception& e )
{
std::cerr << "Exception: " << e.what() << '\n';
}
catch( ... )
{
std::cerr << "Unknown exception caught.\n";
}
}

Rethrowing like this is helpful for using the same error handling code
everywhere without having to supply all the catch blocks each time.

Unlike .NET and Java, for instance, standard C++ does not carry around
a stack trace or anything of that sort, as that would be expensive and
would often be unnecessary/undesirable. Efficiency is preferred run-
time debugability.

Cheers! --M
 
I

Ian Collins

mlimber said:
Right. You must encode any information you want to retrieve in the
exception at the throw point. That can include filename/line number
and anything else. If you've caught by "...", you can rethrow and try
to catch more specifically:

class MyException : public std::exception
{
const char* file_;
const int line_;
const char* desc_;
public:
MyException( const char* file, const int line, const char* desc )
: file_( file ), line_( line ), desc_( desc )
{}

const char* GetFile() const { return file_; }
int GetLine() const { return line_; }
const char* GetDesc() const { return desc_; }

virtual const char* what() const { return desc_; }
};
If you're going to add all this ridiculous getters, just make
MyException a struct and keep things simple.
 
B

Boris

is it possible to receive information like type of exception (division by
zero/null pointer/...), memory adress or source line number in a
catch(...)
statement?

Creating and analyzing application crash dumps might be more helpful to
fix those bugs which slipped through testing and can only be reliably
reproduced by customers. :)

Boris
 
S

Salt_Peter

Hi

is it possible to receive information like type of exception (division by
zero/null pointer/...), memory adress or source line number in a catch(...)
statement?

main()
{
try
{
// do something
} catch(...)
{
// get more information about the exception
}

}

Thanks.
Bernie

derive from std::runtime_error...

#include <iostream>
#include <stdexcept>

class error_divzero : public std::runtime_error
{
const char* pfile;
public:
error_divzero(const char* p, const char* f)
: std::runtime_error(p), pfile(f) { }
// member
const char* what() const throw()
{
return pfile;
}
};

int main()
{
try
{
int x(1);
int y(0);
y ? x/y : throw error_divzero("divide by zero", __FILE__);
}
catch (const error_divzero& r_e )
{
std::cout << "Error: ";
std::cout << r_e.std::runtime_error::what() << std::endl;
std::cout << r_e.what() << std::endl;
}
catch (const std::exception& r_e )
{
std::cout << "Error: ";
std::cout << r_e.what() << std::endl;
}
}

/*
Error: divide by zero
/%somepath%/test.cpp
*/
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top