0

Hii , I am new to C++ programming and need some help regarding the code i wrote below.... Its a basic exception handling program

#include<iostream>

class range_error
 {
      public:
    int i;
    range_error(int x){i=x;}
 } 

 int compare(int x)
  {
              if(x<100)
                  throw range_error(x);
             return x;               
  }                

 int main()
  {
     int a;
     std::cout<<"Enter a  ";
     std::cin>>a;
     try
      {
         compare(a);        
         }
       catch(range_error)
        {
            std::cout<<"Exception caught";
            }
      std::cout<<"Outside the try-catch block";
     std::cin.get();
    return 0;                 
}      

When i compile this ... i get this ...

New types may not be defined in a return type at line 11.(at the start of compare function).

Please explain me what is wrong...

1
  • 1
    I hope your code isn't actually formatted that randomly.
    – GManNickG
    Oct 24, 2010 at 7:14

1 Answer 1

7
class range_error
 {
      public:
    int i;
    range_error(int x){i=x;}
 }; // <-- Missing semicolon.

 int compare(int x)
  {
              if(x<100)
                  throw range_error(x);
             return x;               
  }      

Here's how your code should probably look:

#include <iostream>
#include <stdexcept>

// exception classes should inherit from std::exception,
// to provide a consistent interface and allow clients
// to catch std::exception as a generic exception
// note there is a standard exception class called
// std::out_of_range that you can use.
class range_error : 
    public std::exception 
{
public:
    int i;

    range_error(int x) :
    i(x) // use initialization lists
    {}
}; 

// could be made more general, but ok
int compare(int x)
{
    if (x < 100) // insertspacesbetweenthingstokeepthemreadable
        throw range_error(x);

    return x;               
}                

int main()
{
    int a;
    std::cout<<"Enter a  ";
    std::cin>>a;

    try
    {
        compare(a);        
    }
    catch (const range_error& e) // catch by reference to avoid slicing
    {
        std::cout << "Exception caught, value was " << e.i << std::endl;
    }

    std::cout << "Outside the try-catch block";
    std::cin.get();

    return 0; // technically not needed, main has an implicit return 0         
}
3
  • Thank You. That was helpful .But how did catching by reference avoid slicing ....Please explain ...
    – Flash
    Oct 24, 2010 at 7:33
  • @ravi: If another class had derived form your range_error class, (and perhaps provided a different what() function), if you caught by value, that object is "sliced"; it loses its polymorphic behavior. Also, catching by reference avoids a needless copy. It's a good habit to get into. (It doesn't have to be a const reference.)
    – GManNickG
    Oct 24, 2010 at 8:00
  • It does not have to be const, but it probably should be. Oct 24, 2010 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.