Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programming101

Learn everything about programming

HackerRank Exceptional server solution in c++ programmaing

YASH PAL, 31 July 202422 August 2024

In this HackerRank Exceptional server problem in the c++ programming language, In this challenge, you are required to handle error messages while working with small computational server that performs complex calculations.

It has a function that takes 2 large numbers as its input and returns a numeric result. Unfortunately, there are various exceptions that may occur during execution.

Complete the code in your editor so that it prints appropriate error messages, should anything go wrong. The expected behavior is defined as follows:

  • If the compute function runs fine with the given arguments, then print the result of the function call.
  • If it fails to allocate the memory that it needs, print Not enough memory.
  • If any other standard C++ exception occurs, print Exception: S where S is the exception’s error message.
  • If any non-standard exception occurs, print Other Exception.
HackerRank Exceptional server solution in c++ programming

HackerRank Exceptional server problem solution in c++ programming.

#include <iostream>
#include <exception>
#include <string>
#include <stdexcept>
#include <vector>
#include <cmath>
using namespace std;

class Server {
private:
    static int load;
public:
    static int compute(long long A, long long B) {
        load += 1;
        if(A < 0) {
            throw std::invalid_argument("A is negative");
        }
        vector<int> v(A, 0);
        int real = -1, cmplx = sqrt(-1);
        if(B == 0) throw 0;
        real = (A/B)*real;
        int ans = v.at(B);
        return real + A - B*ans;
    }
    static int getLoad() {
        return load;
    }
};
int Server::load = 0;

int main() {
    int T; cin >> T;
    while(T--) {
        long long A, B;
        cin >> A >> B;

        /* Enter your code here. */
        try
{
    cout<<Server::compute(A,B)<<endl;
}
catch(bad_alloc& error)
{
    cout<<"Not enough memory"<<endl;
}
catch(exception& error)
{
    cout<<"Exception: "<<error.what()<<endl;
}
catch(...)
{
    cout<<"Other Exception"<<endl;
}

    }
    cout << Server::getLoad() << endl;
    return 0;
}

Second solution

#include <iostream>
#include <exception>
#include <string>
#include <stdexcept>
#include <vector>
#include <cmath>
using namespace std;

class Server {
private:
    static int load;
public:
    static int compute(long long A, long long B) {
        load += 1;
        if(A < 0) {
            throw std::invalid_argument("A is negative");
        }
        vector<int> v(A, 0);
        int real = -1, cmplx = sqrt(-1);
        if(B == 0) throw 0;
        real = (A/B)*real;
        int ans = v.at(B);
        return real + A - B*ans;
    }
    static int getLoad() {
        return load;
    }
};
int Server::load = 0;

int main() {
    int T; cin >> T;
    while(T--) {
        long long A, B;
        cin >> A >> B;
try{
    cout << Server::compute(A,B) << endl;
} catch(std::bad_alloc & e) {
    cout << "Not enough memory" << endl;    
} catch(exception & e) {
    cout << "Exception: " << e.what() << endl;
} catch(int e) {
    cout << "Other Exception" << endl;
}
    }
    cout << Server::getLoad() << endl;
    return 0;
}
C++ Programming Tutorials coding problems solutions Hackerrank Problems Solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes