본문 바로가기

Language Proficiency/C++

Exceptional Server

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

2
-8 5
1435434255433 5

Sample Output

Exception: A is negative
Not enough memory
2

Explanation

 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.





try~catch 문을 이용하여 예외 처리를 하는 문제이다. 기존 코드는 작성되어 있고, try~catch 문만 작성하면된다.

이런 문제는 문제 자체를 푸는 것은 쉽기 때문에 그냥 넘어갈 수도 있지만, 문제에 제공되는 다른 부분의 코드 부분을 보면서 배울 수 있는 것도 의외로 많다.


전체 코드를 남겨놓는다.



'Language Proficiency > C++' 카테고리의 다른 글

Abstract Classes - Polymorphism  (0) 2018.08.02
Virtual Functions  (0) 2018.08.02
Inherited Code  (0) 2018.08.02
StringStream  (0) 2018.08.02
Vector-Sort  (0) 2018.08.01