[Ilugc] Exception handling problem(g++)

  • From: mssnlayam@xxxxxxxx (Suriya Narayanan M S)
  • Date: Mon Jul 19 03:55:41 2004

On Mon, Jul 19, 2004 at 02:59:57AM +0530, VENKATRAMAN.D@xxxxxxxxxxxxxx wrote:

 
int main( )
{
    for( int i = 0; i < 3; i++ )
    {
        try {
            throwFunc( i ) ;
        }
        catch( ... ) {
            cout << "exception caught" << endl ;
        }
 
    }// for loop
 
    return( 0 ) ;
}
 

void throwFunc( int i )
{
    int x = 5, y = 0, r = 0 ;
     r = x /y ;
 }
 

throwFunc does not ``throw'' any exception as you expect it to. C++
exceptions are different from processor exceptions. x/y raises a
division by zero exception. You should instead have

if (y == 0) {
    throw EXCEPTION;
}

HTH,
Suriya

Other related posts: