# Java Exception Handling | HackerRank Solution

Hello coders, today we are going to solve Java Exception Handling HackerRank Solution.

## Problem

You are required to compute the power of a number by implementing a calculator. Create a class MyCalculator which consists of a single method long power(int, int). This method takes two integers, n and p, as parameters and finds np. If either n or p is negative, then the method must throw an exception which says “ n or p should not be negative “. Also, if both n and p are zero, then the method must throw an exception which says “n or p should not be zero .
For example, -4 and -5 would result in java.lang.Exception : n or p should not be negative.

Complete the function power in class MyCalculator and return the appropriate result after the power operation or an appropriate exception as detailed above.

## Input Format

Each line of the input contains two integers, n and p. The locked stub code in the editor reads the input and sends the values to the method as parameters.

#### Constraint

• -10 <= n <= 10
• -10 <= p <= 10

## Output Format

Each line of the output contains the result np, if both n and p are positive. If either n or p is negative, the output contains “n and p should be non-negative”. If both n and p are zero, the output contains “n and p should not be zero.”. This is printed by the locked stub code in the editor.

#### Sample Input

``````3 5
2 4
0 0
-1 -2
-1 3``````

#### Sample Output

`````` 243
16
java.lang.Exception: n and p should not be zero.
java.lang.Exception: n or p should not be negative.
java.lang.Exception: n or p should not be negative.``````

#### Explanation

• In the first two cases, both n and p are postive. So, the power function returns the answer correctly.
• In the third case, both n and p are zero. So, the exception, “n and p should not be zero.”, is printed.
• In the last two cases, at least one out of n and p is negative. So, the exception, “n or p should not be negative.”, is printed for these two cases.

## Solution – Java Exception Handling

```import java.util.Scanner;
import java.math.*;
class MyCalculator {
/*
* Create the method long power(int, int) here.
*/

public static long power(int a,int b) throws Exception{
long la = a;
long lb = b;
long c = (long)Math.pow(la,lb);
if(la==0 && lb==0)  {
throw new Exception("n and p should not be zero.");

}
else if(la<0 || lb <0) {
throw new Exception("n or p should not be negative.");

}
else
return c;

}

}

public class Solution {
public static final MyCalculator my_calculator = new MyCalculator();
public static final Scanner in = new Scanner(System.in);

public static void main(String[] args) {
while (in .hasNextInt()) {
int n = in .nextInt();
int p = in .nextInt();

try {
System.out.println(my_calculator.power(n, p));
} catch (Exception e) {
System.out.println(e);
}
}
}
}```

Disclaimer: The above Problem ( Java Exception Handling ) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

### 2 thoughts on “Java Exception Handling | HackerRank Solution”

1. Thank you. I’m not sure why HackerRank marked the solution as failing test case #1 (Wrong Answer).

2. When I changed long to int (int power(int n, int p) instead of long power(int n, int p)), it works (all cases passed). So, obviously the problem is not 100% correct.