We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 17: More Exceptions
Day 17: More Exceptions
Sort by
recency
|
618 Discussions
|
Please Login in order to post a comment
class Calculator{ int n, p; public Calculator(){} public int power(int n, int p) throws Exception{ if(n<0 || p<0) throw new Exception("n and p should be non-negative"); else{ return (int) Math.pow(n,p); } } }
In C#
My 2 cents in Java:
python3
import math
class Calculator: def init(self): pass
myCalculator=Calculator() T=int(input()) for i in range(T): n,p = map(int, input().split()) try: ans=myCalculator.power(n,p) print(ans) except Exception as e: print(e)