exception handling

can we throw different type of object in re throw ?

i dont understand what re throw means.. amd hence the answer for this question..
Would be great if someone could explain
Hi,

If you are not handling a particular type of exception then you can throw it again so that some other functions can handle it. The syntax of re-throw is just a throw statement where you will not be able to specify any object.

throw;

If you want to throw a different type of exception, then catch the first exception and then throw your new object with a regular throw statement.

1
2
3
4
5
6
7
8
9
try{
         aFunctionThatThrowsException();

}
catch(Exception& e)
{
         throw MyNewException();
}
my addendum to athachil's good answer:
1. make sure what does it mean by "rethrow" in your interview question.
2. if "rethrow" means "throw;", then you have no choice but to throw the "same" object again. even this exception object may be modified in this level of catch, but "throw;" will not record any modification and throw only the original object which is catched by this level.
3. see athachil's last example.
ok... Now.. I thought about this... lets say we have the following scenario


1
2
3
4
5
6
7
8
9
10
11
12
function Somefunction()
{
      try{
         // some code that throws an exception
         aFunctionThatThrowsException();
      }
     catch(Exception& e)
     {
         throw MyNewException();
     } 

}



So in this case, you might have to the exception thrown by the code in someFunction diferently than that thrown by the aFunctionThatThrowsException()..

So in such case if put throw MyNewException(); in the catch block then the new exception would be thrown rite and the function that invokes Somefunction() would always see the myNewException irrespective of who throws the exception...

How do we deal with such scenarios?
When would a single exception suddenly evolve into some other exception? Although I beleive users could do this, I don't think they should.
@moorecm..
I am not talking abt a single exception transforming into a new one..!! but what i mean is, who do we deal with exceptions thrown by two different parts of the code..

for example.. this scenario is easy..

1
2
3
4
5
6
7
8
9
10
11
12
13
function Somefunction()
{
      try{
         // some code that throws an exception
         aFunctionThatThrowsException();
         oneMoreFunctionThatThrowsException();
      }
     catch(Exception& e)
     {
         throw;
     } 

}



In the above case we just throw the exception.. So you are just passing on the exception you get.. So no problem.. The problem is in the above case where I want to deal the exception that occurs in the local code and pass on the exception that is caught from aFunctionThatThrowsException();

Hope i am clear now..!
In that scenario, two things come to mind. First, you could use different try and catch blocks for your local code than for the function calls. Second, you do not have to catch all exceptions so that some of them propagate to another catch.
std::exception has a virtual destructor. It practically begs you to derive other exceptions from it.

( http://cplusplus.com/reference/std/exception/exception/ )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <exception>
using namespace std;

class ExA:public exception {
public:
    const char * what() const throw() {
        return "ExA!";}
};

class ExB:public exception {
public:
    const char * what() const throw() {
        return "ExB!";}
};

void fA() {throw ExA();}
void fB() {throw ExB();}
void fC() {throw exception();}

int main()
{
    try {
        //uncomment one at a time
        //and watch what happens

        //fA();
        //fB();
        //fC();
        //throw 0;
    }
    catch (ExA & e) {
        cerr << e.what();
        cerr << "\nhandling ExA...\n";
    }
    catch (ExB & e) {
        cerr << e.what();
        cerr << "\nhandling ExB...\n";
    }
    catch (exception & e) {
        cerr << e.what();
        cerr << "\nhandling std::exception...\n";
    }
    catch (...) {
        cerr << "handling unknown exception...\n";
    }

    cout << "hit enter to quit...\n";
    cin.get();
    return 0;
}
Last edited on
Thats great.. then its kind of java exception catching!! .. Now it does make sense...

But my next question is how do we catch a divide by zero exception..??

1
2
3
4
5
6
7
8
9
try
   {
             int b=10;
             b-=10;
             int a=b/0;
   }
   catch(...)
   {}
   


The above code causes System.DivideByZeroException (I use Visual Studio 2010) how to we catch that?

Question 2
========

Are the type of exception thrown compiler dependent??

Question 3
========
How do we find the list of exceptions that a compiler/C++ throws ?
karthick88 wrote:
The above code causes System.DivideByZeroException (I use Visual Studio 2010) how to we catch that?

Are you sure it throws that? Try catch(System::DivideByZeroException & e) {/*...*/} and see if it works. If it does, then you're right.

In general, there are two kinds of exceptions, hardware and software ones (much like assembly interrupts). Hardware exceptions are raised by the CPU, while software exceptions are raised by applications and the OS. The easy thing you can do with your compiler is catch the software exceptions raised by your application. If you want to catch OS raised exceptions or even hardware exceptions you'll have to struggle a bit more...

http://msdn.microsoft.com/en-us/library/ms680657%28VS.85%29.aspx
http://www.codeproject.com/kb/cpp/exceptionhandler.aspx
Last edited on
Topic archived. No new replies allowed.