C++ - struct file_exception {}

P

pbartosz

#include <iostream>

struct exception {};
struct file_exception: public exception {

};

void ex() throw(file_exception) {
throw new file_exception();

}

int main()
{
try {
ex();
}
catch (file_exception) {
std::cout << "catched\n";
}
return 0;

}

I get the message: "terminate called after throwing an instance of
'file_exception*'".
Why can't I catch the file_exception?
 
M

Michael Doubez

#include <iostream>

struct exception {};
struct file_exception: public exception {

};

void ex() throw(file_exception) {
        throw new file_exception();

}

int main()
{
        try {
                ex();
        }
        catch (file_exception) {
                std::cout << "catched\n";
        }
        return 0;

}

I get the message: "terminate called after throwing an instance of
'file_exception*'".
Why can't I catch the file_exception?

Because you are throwing a pointer to a file_exception, try:
throw file_exception();
 
I

Ian Collins

#include<iostream>

struct exception {};
struct file_exception: public exception {

};

You have made just about every mistake there is to make with exceptions!
void ex() throw(file_exception) {
throw new file_exception();

1) Don't use exceptions specifications, they are more trouble than they
are worth. Here you say you will only throw a file_exception, but you
throw a file_exception*.

2) Never throw a pointer to an exception.
}

int main()
{
try {
ex();
}
catch (file_exception) {

Never catch by value, always use a cost reference.
std::cout<< "catched\n";
"caught".

}
return 0;

}

I get the message: "terminate called after throwing an instance of
'file_exception*'".
Why can't I catch the file_exception?

Because you didn't throw what you said you would.
 
P

pbartosz

1) Don't use exceptions specifications, they are more trouble than they
are worth.  Here you say you will only throw a file_exception, but you
throw a file_exception*.

Thanks, I forgot C++ new operator return a pointer, otherwise than
Java new operator.
Never catch by value, always use a cost reference.

Oh, I accidentally changed it while writing post.

Thanks for response.
 
P

pbartosz

1) Don't use exceptions specifications, they are more trouble than they
are worth. Here you say you will only throw a file_exception, but you
throw a file_exception*.

Thanks, I forgot that C++ new operator returns a pointer, otherwise
than
Java new operator.
Never catch by value, always use a cost reference.

Oh, I accidentally changed it before writing post.

Thanks for response.
 
P

pbartosz

1) Don't use exceptions specifications, they are more trouble than they
are worth. Here you say you will only throw a file_exception, but you
throw a file_exception*.

Thanks, I forgot that C++ new operator returns a pointer, otherwise
than Java new operator.
Never catch by value, always use a cost reference.

Oh, I accidentally changed it before writing post.

Thanks for response.
 
I

Ian Collins

Thanks, I forgot that C++ new operator returns a pointer, otherwise
than Java new operator.


Oh, I accidentally changed it before writing post.

Thanks for response.

Your send button appears to be stuck.....
 

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
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top