<\/span><\/h2>\n\n\n\nFor 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.<\/p>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\n2\n-8 5\n1435434255433 5\n<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\nException: A is negative\nNot enough memory\n2\n<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h2>\n\n\n\n-8<\/strong> is negative, hence ‘Exception: A is negative’<\/strong> is thrown. Since the second input is too large, ‘not enough memory’ is displayed. 2<\/strong> is the server load.<\/p>\n\n\n\n<\/span>Solution – Exceptional Server HackerRank Solution in C++<\/strong><\/span><\/h2>\n\n\n\n#include <iostream>\n#include <exception>\n#include <string>\n#include <stdexcept>\n#include <vector>\n#include <cmath>\nusing namespace std;\n\nclass Server {\nprivate:\n\tstatic int load;\npublic:\n\tstatic int compute(long long A, long long B) {\n\t\tload += 1;\n\t\tif(A < 0) {\n\t\t\tthrow std::invalid_argument(\"A is negative\");\n\t\t}\n\t\tvector<int> v(A, 0);\n\t\tint real = -1, cmplx = sqrt(-1);\n\t\tif(B == 0) throw 0;\n\t\treal = (A\/B)*real;\n\t\tint ans = v.at(B);\n\t\treturn real + A - B*ans;\n\t}\n\tstatic int getLoad() {\n\t\treturn load;\n\t}\n};\nint Server::load = 0;\n\nint main() {\n\tint T; cin >> T;\n\twhile(T--) {\n\t\tlong long A, B;\n\t\tcin >> A >> B;\n\n\t\t\/* Enter your code here. *\/\n try {\n cout << Server::compute(A,B) << endl;\n } \n catch (bad_alloc& error) {\n cout << \"Not enough memory\" << endl;\n }\n catch (exception& error) {\n cout << \"Exception: \" << error.what() << endl;\n }\n catch (...) {\n cout << \"Other Exception\" << endl;\n }\n\n\t}\n\tcout << Server::getLoad() << endl;\n\treturn 0;\n}\n<\/pre>\n\n\n\n