Skip to content
the poor coder

Hackerrank Exceptional Server Solution

Poor Man
Published
Updated
Reading time
2 min read

Your friend set up a small computational server that performs complex calculations.
It has a function that takes  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  is the exception's error message.
  • If any non-standard exception occurs, print Other Exception.

Input Format

The first line contains an integer, , the number of test cases.
Each of the  subsequent lines describes a test case as  space-separated integers,  and , respectively.

Constraints


Output Format

For each test case, print a single line containing whichever message described in the Problem Statement above is appropriate. After all messages have been printed, the locked stub code in your editor prints the server load.

Sample Input

Text
2
-8 5
1435434255433 5

Sample Output

Text
Exception: A is negative
Not enough memory
2

Explanation.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

is negative, hence 'Exception: A is negative' is thrown. Since the second input is too large, 'not enough memory' is displayed.  is the server load.

Solution in cpp#

Approach 1.

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

Approach 2.

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

Approach 3.

Python
        
        try{  
            int res = Server::compute(A,B);
            cout << res << endl;
        } catch (const bad_alloc& ex) {
            cout << "Not enough memory" << endl;
        } catch (const exception& ex) {
            cout << "Exception: " << ex.what() << endl;
        } catch (...){  
            cout << "Other Exception" << endl;
        }     
 

Keep going

2020

Hackerrank Virtual Functions Solution

This problem is to get you familiar with virtual functions. Create three classes Person, Professor and Student. The class Person should have data members name and age. The classes Professor and Student should inherit…

2020

Hackerrank Inherited Code Solution

You inherited a piece of code that performs username validation for your company's website. The existing function works reasonably well, but it throws an exception when the username is too short. Upon review, you…

2020

Hackerrank Box It! Solution

Design a class named Box whose dimensions are integers and private to the class. The dimensions are labelled: length , breadth , and height . The default constructor of the class should initialize , , and to . The…

ESC