Comprehensive listing of standard C++ exceptions

U

usenet

I am trying to collect a comprehensive listing of standard C++
exceptions. This is what I have got so far (by going through the
standard). Kindly let me know if I missed anything.

Thanks,
Ramesh

//
// ********* Derived from std::exception *********
//
class bad_alloc; // thrown to report a failure to allocate storage

class bad_cast; // thrown to report an invalid dynamic cast
expression

class bad_typeid; // thrown to report a null pointer in a typeid
expression

class bad_exception; // thrown when an exception type doesn't match
any catch



class logic_error(const string& what_arg); // thrown to report
errors presumably
// detectable before the
program
// executes, such as
violations of
// logical preconditions or
// class invariants

class runtime_error(const string& what_arg); // thrown to report
errors presumably
// detectable only when
the program
// executes


class ios_base::failure(const string& msg); // thrown by functions
in the iostreams
// library, to report
errors detected during
// stream buffer
operations.
//
// ********* Derived from logic_error *********
//
class domain_error(const string& what_arg); // thrown to report
domain errors

class invalid_argument(const string& what_arg); // thrown to report an
invalid argument

class length_error(const string& what_arg); // thrown to report an
attempt to produce
// an object whose
length exceeds its
// maximum allowable
size

class out_of_range(const string& what_arg); // thrown to report an
argument value not in
// its expected range


//
// ********* Derived from runtime_error *********
//
class range_error(const string& what_arg); // thrown to report
range errors in
// internal
computations

class overflow_error(const string& what_arg); // thrown to report an
arithmetic overflow error

class underflow_error(const string& what_arg); // thrown to report an
arithmetic underflow error
 
G

Generic Usenet Account

What does the C++ standard say about statements after throw? My
compiler (g++ version 3.3.1) does not complain even if there are
statements after an unconditional throw statement (see sample code
below)

Thanks,
Ramesh

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <stdexcept>

using namespace std;

#define DEBUG


void
foo() throw (logic_error)
{
throw logic_error("Function foo called");
cerr << "Statement after throw ---- compiler should complain\n";
}


void
bar() throw (runtime_error)
{
try
{
foo();
}
catch(exception& xcptn)
{
#ifdef DEBUG
cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ",
" << __LINE__ << endl;
#endif
throw runtime_error(string("Exception in function ") + __func__ +
string("()"));
cerr << "Statement after throw ---- compiler should complain\n";
}
}

main()
{
try
{
bar();
}
catch(exception& xcptn)
{
cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ",
" << __LINE__ << endl;
}

return 0;
}
 
K

Kai-Uwe Bux

Generic said:
What does the C++ standard say about statements after throw?

As far as I can see: nothing.
My compiler (g++ version 3.3.1) does not complain even if there are
statements after an unconditional throw statement (see sample code
below)

Thanks,
Ramesh

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <stdexcept>

using namespace std;

#define DEBUG


void
foo() throw (logic_error)
{
throw logic_error("Function foo called");
cerr << "Statement after throw ---- compiler should complain\n";

Why? As far as I can tell, there is nothing wrong with the code. It's like

if ( false ) {
cerr << "this does not print.\n";
}
}


void
bar() throw (runtime_error)
{
try
{
foo();
}
catch(exception& xcptn)
{
#ifdef DEBUG
cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ",
" << __LINE__ << endl;
#endif
throw runtime_error(string("Exception in function ") + __func__ +
string("()"));
cerr << "Statement after throw ---- compiler should complain\n";

Again, there is no reason for the compiler to complain.
}
}

main()

Now, here the compiler should complain: main returns int.
{
try
{
bar();
}
catch(exception& xcptn)
{
cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ",
" << __LINE__ << endl;
}

return 0;
}

Compilers are free to issue warnings about anything that looks fishy;
however, the standard does not seem to require any diagnostics related to
unreachable code.


Best

Kai-Uwe Bux
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top