0

The following program will get the error of

unreported exception Exception; must be caught or declared to be thrown

Why the re-throw is not working? In C#, there is no need to wrap the call in try block.

public class HelloWorld{

    public static double test() throws Exception {
        return 1;
    }

    public static void main(String []args){
        try {
            double a = test();
            System.out.println(a);
        }
        catch (Exception e) {
            throw e;
        }
    }
}
ca9163d9
  • 27,283
  • 64
  • 210
  • 413

3 Answers3

4

C# doesn't have checked exceptions, so this compile error can't occur there.

In Java if you want to throw a checked exception from a method you have to add that exception to the throws clause, here you would change the main method to:

public static void main(String[] args) throws Exception

When an exception is thrown from the main method, the JVM will write the stacktrace to stderr.

In general wrapping the exception in an unchecked exception (RuntimeException) is possible too. In this particular case, since this is being thrown from the main method and propagation of the checked Exception is not an issue, all that wrapping the exception accomplishes is it generates a bigger stacktrace.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • "...you have to add that exception to the throws clause" *or* catch and throw a `RuntimeException` which stops the propagation of the evil checked exception. – MadConan May 21 '15 at 18:22
  • @MadConan: that would work too, propagating checked exceptions is definitely bad.. – Nathan Hughes May 21 '15 at 18:23
  • Propagating *any* exceptions beyond `main` is bad. Why create a runtime exception? The proper way to deal with an exception is to *deal* with it. – RealSkeptic May 21 '15 at 18:23
  • @RealSkeptic I disagree. There are going to be plenty of cases when you can't do anything -- even in `main`. At which point, all you have left is throwing it. The JVM itself will handle that. Not every app needs logging beyond the console. – MadConan May 21 '15 at 18:25
  • @RealSkeptic: for a small program run from the command line this might not be too bad, depending. anyway, the question seems to be about why the OP's code can't throw the exception from the main method. – Nathan Hughes May 21 '15 at 18:28
  • @NathanHughes Small tests and stuff - yes. I wasn't disagreeing with your answer, it's the correct one given the error and the puzzlement of the OP. I was disagreeing with the idea of bypassing this with a `RuntimeException`. – RealSkeptic May 21 '15 at 18:32
2

You need to try throw new RuntimeException(e) instead of throw e. Throwing Exception will give a compile time error as it will require you to deal with the checked exception. new RuntimeException(e) will basically wrap your checked exception and throw it, aborting the program.

Harsh Poddar
  • 2,394
  • 18
  • 17
0

If main() throws an exception and does not catch it then main() must have a throws declaration.

Arthur Laks
  • 524
  • 4
  • 8