I am new to programming in JAVA and got stuck in code where I need to print the the exponential result of two non-negative numbers. In case if any of them is negative, I need to throw an exception, my code is as follows:`
import java.util.*;
import java.util.Scanner;
class MyCalculator {
int power(int n, int p) {
int result = 1;
if (n < 0 || p < 0) {
throw new Exception("n and p should be non-negative");
else
{
while(p!=0)
{
result=result*n;
p-=1;
}
return result;
}
}
}
class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int n = in.nextInt();
int p = in.nextInt();
MyCalculator my_calculator = new MyCalculator();
try {
System.out.println(my_calculator.power(n, p));
} catch (Exception e) {
System.out.println(e);
}
}
}
}
I am getting the above written error IE:
error: unreported exception Exception; must be caught or declared to be thrown
I needed a conceptual understanding of what actually is causing this error to occur.