Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Cpp exception handling solution in C++ programming

YASH PAL, 31 July 202416 January 2026

In this HackerRank Cpp exception handling problem solution in the C++ programming language, In this challenge, the task is to debug the existing code to successfully execute all provided test files.

You are required to extend the existing code so that it handles the std::invalid_argument exception properly. More specifically, you have to extend the implementation of the process_input function. It takes an integer n as an argument and has to work as follows:

  1. It calls function largest_proper_divisor(n).
  2. If this call returns a value without raising an exception, it should print in a single line result=d where d is the returned value.
  3. Otherwise, if the call raises a std::invalid_argument exception, it has to print in a single line the string representation of the raised exception, i.e. its message.
  4. Finally, no matter if the exception is raised or not, it should print in a single line returning control flow to caller after any other previously printed output.

To keep the code quality high, you are advised to have exactly one line printing returning control flow to caller in the body of process_input function.

Your function will be tested against several cases by the locked template code.

HackerRank Cpp exception handling solution in c++ programming

HackerRank Cpp exception handling problem solution in c++ programming.

#include <iostream>
#include <stdexcept>

using namespace std;

int largest_proper_divisor(int n) {
    if (n == 0) {
        throw invalid_argument("largest proper divisor is not defined for n=0");
    }
    if (n == 1) {
        throw invalid_argument("largest proper divisor is not defined for n=1");
    }
    for (int i = n/2; i >= 1; --i) {
        if (n % i == 0) {
            return i;
        }
    }
    return -1; // will never happen
}

void process_input(int n) {
    try{
        int d = largest_proper_divisor(n); 
        cout << "result=" << d << endl;
    }
    /*class invalid_argument : public logic_error {
    public:
        explicit invalid_argument (const string& what_arg);
        explicit invalid_argument (const char* what_arg);
    };
    }*/
    catch (invalid_argument& ia) {
        cout<<ia.what()<<'n';
    }
    catch(...){

    }
    cout << "returning control flow to caller"<<'n';
}


int main() {
    int n;
    cin >> n;
    process_input(n);
    return 0;
}

C++ Solutions coding problems solutions Hackerrank Problems Solutions cppHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Say “Hello, World!” with C++ solution
Input and Output solution
Basic Data Types solution
Conditional Statements solution
For loop solution
Functions solution
Pointer solution
Arrays Introduction solution
Variable-Sized Arrays solution
Attribute Parser solution
StringStream solution
Strings solution
Structs solution
Class solution
classes and objects solution
Box It! solution
Inherited code solution
Exceptional server solution
Virtual Functions solution
Abstract Classes — Polymorphism solution
Vector-Sort solution
Vector-Erase solution
Lower Bound-STL solution
Sets-STL solution
Maps-STL solution
Print Pretty solution
Deque-STL solution
Inheritance Introduction solution
Hotel Prices solution
Cpp exception handling solution
Rectangle Area solution
Multi-Level Inheritance solution
Overloading Ostream Operator solution
Messages Order solution
Accessing Inherited Functions solution
Magic Spells solution
C++ Class Templates problem solution
Preprocessor Solution
Operator Overloading solution
Overload Operators solution
Attending workshops solution
C++ Class Template Specialization solution
C++ Variadics problem solution
Bit Array solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes