Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3285316
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:15:12+00:00 2026-05-17T20:15:12+00:00

Hii , I am new to C++ programming and need some help regarding the

  • 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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:15:13+00:00Added an answer on May 17, 2026 at 8:15 pm
    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         
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hii....i am new to iPhone programming..Can anybody help me out please i have an
Hi I am quite new to php but i have been following some tutorials
Hi I am new to web programming. I want to create a template sort
Hi I am new to android Programming and NDK.Yet i am not clear with
hii i am new to iPhone world i am trying to trap the backspace
Hii, I am new to architectural designing of the applications, and our company is
hii every one, following is my code which displays 4 panel one is at
Hi I am new and I am used to MS SQL Manager's Attach/Detatch functionality
Hi I'm very new to sql but have been passed a job in which
Hi i need to generate 9 digit unique account numbers. Here is my pseudocode:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.