1

I am getting an error Error:(25, 13) java: unreported exception ExceptionA; must be caught or declared to be thrown when trying to rethrow the exception.

I am still new to Java and my book uses the below to "rethrow," but I get an error at throw exception; when attempting to rethrow even though this is based directly off of the book.

import java.io.IOException;

public class catchingExceptions {
    public static void main(String args[]) {
        throwExceptionA();
        throwExceptionB();
        throwNullPointer();
        throwIOException();
    }

    public static void throwExceptionA() {
        try {
            throw new ExceptionA();
        } catch (ExceptionA exception) {
            System.err.printf("Exception: %s \n", exception);
            System.err.println("ExceptionA handled in method throwExceptionA \n");
            throw exception;
        }
    }

    public static void throwExceptionB() {
        try {
            throw new ExceptionB();
        } catch (ExceptionB exception) {
            System.err.printf("Exception: %s \n", exception);
            System.err.println("ExceptionB handled in method throwExceptionB \n");
        }
    }

    public static void throwNullPointer() {
        try {
            throw new NullPointerException();
        } catch (NullPointerException exception) {
            System.err.printf("Exception: %s \n", exception);
            System.err.println("NullPointerException handled in method throwNullPointerException \n");
        }
    }

    public static void throwIOException() {
        try {
            throw new IOException();
        } catch (IOException exception) {
            System.err.printf("Exception: %s \n", exception);
            System.err.println("IOException handled in method throwIOException \n");
        }
    }
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79

2 Answers2

2

In Java, any method that can throw a checked exception must declare this, so change

public static void throwExceptionA()

to

public static void throwExceptionA() throws ExceptionA

This is not necessary for unchecked exceptions.

Whether an exception is checked or unchecked depends on what class it inherits from.

  • If the exception inherits directly or indirectly (i.e. through an inheritage chain) from RuntimeException, it is considered unchecked and doesn't need to be declared.
  • All other exceptions (i.e. such inheriting directly or indirectly from Exception, but never from RuntimeException) are considered checked and need to be declared if they can be thrown by the method.

With this in mind, consider these examples:

public void throwsUndeclaredCheckedException() {
    // compiler error because this exception is not declared (and not caught)
    throw new Exception();
}

public void throwsDeclaredCheckedException() throws Exception {
    // okay, because it was declared
    throw new Exception();
}

public void catchesUndeclaredCheckedException() {
    try {
        throw new Exception();
    } catch( Exception ignored ) {
        // the thrown exception is caught and now ignored, hence the method
        // can't throw and we don't need to declare anything
    }
}

public void throwsUndeclaredRuntimeException() {
    // okay, because it's an unchecked exception
    throw new RuntimeException();
}

public void throwsDeclaredRuntimeException() throws RuntimeException {
    // works, but the "throws" declaration on the method is unnecessary
    throw new RuntimeException();
}

You can find a discussion on the differences, for example, here

Community
  • 1
  • 1
Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100
  • A better answer than mok's: it gives the answer code **and** explains the problem. 1+ – Hovercraft Full Of Eels Apr 21 '14 at 13:21
  • Thanks for the input and examples. However, the "template" I am working from is now above in the code section. Now I get an error at throwExceptionA();. I know there are more simple ways of doing this, but I most follow the template. –  Apr 21 '14 at 13:38
  • @OptimusJarrod I'm confused, the code currently in the question still has exactly the problem I describe in the beginning of my post. `throwExceptionA` must either catch the exception (without rethrowing it!) or it must declare it. If your book claims that this should compile, then the book is simply wrong (assuming `ExceptionA` really is the same as in the book). – Ingo Bürk Apr 21 '14 at 13:47
  • @OptimusJarrod Maybe you copied `ExceptionA` wrong. Have a close look whether `ExceptionA` inherits from `Exception` or `RuntimeException` in the book. – Ingo Bürk Apr 21 '14 at 13:50
  • @IngoBürk I am using the book for the re-throw, just "throw exception;". However, the template is a template provided that we work from. Therefore, it varies slightly from the book. However, ExceptionA is exactly from the template. I will review it with the teacher I suppose. Thanks for the assistance! –  Apr 21 '14 at 14:00
  • @OptimusJarrod Rethrowing is okay, but then you need to declare it on the method (that's why you're getting the compile error). But yes, talking to your teacher might be an idea – if your template is wrong, other students might have the same problem. Keep us up to date if you know more. Also consider voting up or accepting the answer if it helped you / solved the problem. Cheers! – Ingo Bürk Apr 21 '14 at 14:03
  • 1
    @IngoBürk I am too new to Vote Up, but I did accept the answer. –  Apr 21 '14 at 14:10
0

try this:

public static void throwExceptionA() throws ExceptionA
{
    try
    {
        throw new ExceptionA();
    }
    catch ( ExceptionA exception)
    {
        System.err.printf("Exception: %s \n", exception);
        System.err.println ("ExceptionA handled in method throwExceptionA \n" );
        throw exception;
    }
}
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66