Day 17 More Exceptions Hackerrank Solution in C++. Yesterday's challenge taught you to manage exceptional situations by using try-and-catch blocks. In today's challenge, you will practice throwing and propagating an exception. Check out the Tutorial tab for learning materials and an instructional video.
Task
Write a Calculator class with a single method: int power(int, int). The power method takes two integers, n and p, as parameters and returns the integer result of n^p. If either n or p is negative, then the method must throw an exception with the message: n and p should be non-negative.
Note: Do not use an access modifier (e.g.: public) in the declaration for your Calculator class.
Input Format
Input from stdin is handled for you by the locked stub code in your editor. The first line contains an integer, T, the number of test cases. Each of the T subsequent lines describes a test case in 2 space-separated integers that denote n and p, respectively.
Constraints
No Test Case will result in overflow for correctly written code.
Output Format
Output to stdout is handled for you by the locked stub code in your editor. There are T lines of output, where each line contains the result of n^p as calculated by your Calculator class' power method.
Sample Input
4
3 5
2 4
-1 -2
-1 3
Sample Output
243
16
n and p should be non-negative
n and p should be non-negative
Day 17 More Exceptions Hackerrank Solution in C++
#include <cmath>
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
//Write your code here
class Calculator
{
public:
int power(int first, int second)
{
if (first < 0 || second < 0)
{
throw runtime_error("n and p should be non-negative");
}
return pow(first, second);
}
};
int main()
{
Calculator myCalculator = Calculator();
int T, n, p;
cin >> T;
while (T-- > 0)
{
if (scanf("%d %d", &n, &p) == 2)
{
try
{
int ans = myCalculator.power(n, p);
cout << ans << endl;
}
catch (exception & e)
{
cout << e.what() << endl;
}
}
}
}
Explanation of More Exceptions
Basically in this problem two parameters will be given N and P and we have to calculate N^P as we know that we can not find the root and square root of a negative number or in this problem while N and P are positive programs will work fine and if any N and P were Negative than Run Time error occurs for handling this type of error(Run Time) we use Handler called Exception.
As the given input first line is the total number of inputs then the First input, a second input, third input and the last fourth input. The first input is 3 (N) and 5 (P) so N^P or 3^5 is equal to 243 same for the second input 2^4 is equal to 16 But in the third input (-1)^(-2) this can't be for avoid this type of error(Run Time Error).
As the given input first line is the total number of inputs then the First input, a second input, third input and the last fourth input. The first input is 3 (N) and 5 (P) so N^P or 3^5 is equal to 243 same for the second input 2^4 is equal to 16 But in the third input (-1)^(-2) this can't be for avoid this type of error(Run Time Error).
We use Try-Catch(Exception Handler). Now if the user enters third input then our program finds a runtime error and throws control to catch block(print the message in the catch block).
Syntax of Try-Catch
try
{
statement......1
statement......2
statement......3
.
.
.
statement......n
}
catch(exception )
{
statements //message
}
Syntax of Try-Catch
try
{
statement......1
statement......2
statement......3
.
.
.
statement......n
}
catch(exception )
{
statements //message
}
0 Comments: