exceptions

T

Tony Johansson

Hello!

I have this class definition DuplicateNumberExceptionand and a main below.
A question about this class DuplicateNumberException is it any advantage to
let this class inherit from
the base class exception or can this class DuplicateNumberExceptionand be a
stand alone class like it is here.

//Tony


class DuplicateNumberException
{
public:
DuplicateNumberException(const string&);
void show();

private:
string reason:
};

I have this main here
int main
{
......
......
......
Student s("John")
try
{
Registrar::instance(),insert(s);
}
catch(DuplicateNumberException& e)
{
// can't open
}
return 0;
}
 
K

Kanenas

Hello!

I have this class definition DuplicateNumberExceptionand and a main below.
A question about this class DuplicateNumberException is it any advantage to
let this class inherit from
the base class exception or can this class DuplicateNumberExceptionand be a
stand alone class like it is here.
[...]

Section 17 of the FAQ covers exceptions, including a reason why to
include user-defined exceptions in the 'exception' hierarchy.
http://www.parashift.com/c++-faq-lite/

As for DuplicateNumberException, it should probably (indirectly)
descend from runtime_error (which is declared in <stdexcept>).

Kanenas
 
R

Ron Wills

At Sat, 21 May 2005 15:17:27 GMT,
Tony said:
Hello!

I have this class definition DuplicateNumberExceptionand and a main below.
A question about this class DuplicateNumberException is it any advantage to
let this class inherit from
the base class exception or can this class DuplicateNumberExceptionand be a
stand alone class like it is here.

This is just in my experience. I develop large automation software
suites and I really don't like it when stand alone classes are thrown
for exceptions. The simple reason is that several libraries may be
used and can throw a lot of different exceptions. If a base exception
class is used, then all exceptions can then at least be caught and
logged, instead of unexpectedly aborting the application or ending up with
monster try/catch clauses. It's a way of playing nice with others ;)
 

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

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top