-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path30-more-exceptions.cpp
45 lines (40 loc) · 1.01 KB
/
30-more-exceptions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Tutorials > 30 Days of Code > Day 17: More Exceptions
// Throw an exception when user sends wrong parameters to a method.
//
// https://www.hackerrank.com/challenges/30-more-exceptions/problem
//
#include <cmath>
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
// (skeliton_head) ----------------------------------------------------------------------
//Write your code here
class Calculator
{
public:
int power(int n, int p)
{
if (n < 0 || p < 0)
throw invalid_argument("n and p should be non-negative");
return (int) pow(n, p);
}
};
// (skeliton_tail) ----------------------------------------------------------------------
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;
}
}
}
}