0

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.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

2 Answers2

3

First You are putting else in the wrong place use this instead :

int result = 1;
if (n < 0 || p < 0) {
    throw new Exception("n and p should be non-negative");
} else {
^---------------------------------You have to close the if, then use else
    while (p != 0) {
        result = result * n;
        p -= 1;
    }
    return result;
}

Second your method should be throws Exception

int power(int n, int p) throws Exception {
Graham
  • 7,431
  • 18
  • 59
  • 84
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Good points, but I would leave out the catch example. Empty catch blocks are never a good idea, and throw followed by empty catch is super crazy. – GhostCat May 25 '17 at 09:24
  • thank you @GhostCat so can the `ex.printStackTrace();` do the trick, or what should i do to not let it empty? – Youcef LAIDANI May 25 '17 at 09:27
  • It simply doesn't make sense to catch in this context. You could show how a user of pow() catches. – GhostCat May 25 '17 at 10:18
  • yes this is correct @GhostCat the OP already use `try { System.out.println(my_calculator.power(n, p)); } catch (Exception e) { System.out.println(e); }` so he don't need to use this again in pow() method – Youcef LAIDANI May 25 '17 at 10:28
2

Exception classes like Exception derive from Throwable. There are checked exceptions like the Exception and unchecked exceptions like IllegalArgumentException.

If you would have used the latter, the exception would be unvisible.

With a checked exception the compiler forces you to have a throws ... or to catch the exception.

Here an IllegalArgumentException would fit perfectly.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • How to know whether an Exception is a checked or not in official documentations? – efkan Jan 10 '18 at 19:33
  • 2
    **Unchecked exceptions** extend RuntimeException, that extend Exception that extend Throwable. **Checked exceptions** extend Exception, not RuntimeException. And the rare **Errors** extend Throwable, not Exception. Seen at the top of the javadoc. – Joop Eggen Jan 11 '18 at 07:51