0

I am working with the following code in Eclipse:

public class Foo {
    public static final Bar bar = new Bar(20);
}

public class Bar {
    public int value;

    // This needs to be able to be called in places other than
    // just Foo, and there it will need to throw.
    public Bar(int value) throws Exception {
        if(value == 0) {
            throw Exception("Error. Value cannot be 0 when constructing Bar.");
        }
        return;
     }
}

This gives me an error message in Foo (line 2) which says, "Unhandled exception type Exception", even though in practice, this exception will never occur with this code. Can I disable this error in Eclipse so it doesn't bother me, or is there another way I can deal with this error?

Thanks ahead of time for the answers!

Nicholas
  • 570
  • 1
  • 5
  • 15
  • 1
    Nothing in the source shown references `InvalidCharacterException`, and it's also not part of the standard Java runtime, so where is it being referenced? You knowing that an exception won't be thrown doesn't change the semantics of the method being declared as possibly throwing an exception. – nitind Oct 20 '18 at 05:12
  • 1
    If you don't want to have to catch the exception use an [unchecked exception](https://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) such as `IllegalArgumentException` – greg-449 Oct 20 '18 at 06:39

2 Answers2

1

This is a compiler bug that needs to be fixed to compile the Java code and is not an Eclipse issue: checked exceptions require an explicit handling in Java by surrounding it with try-catch or pass the exception (method/constructor throws ...).

If the class Bar cannot be changed, one possibility would be to use a private static method to initialize the constant bar (which should be named BAR according to the Java naming conventions):

public class Foo {

    public static final Bar BAR = initBar(20);

    private static Bar initBar(int value) {
        try {
            return new Bar(20);
        } catch (InvalidCharacterException e) {
            return null;
        }
    }

}
howlger
  • 31,050
  • 11
  • 59
  • 99
0

Surround the constructor with a try/catch to catch the exception.

Like this:

public class Foo {

try {
    public static final Bar = new Bar(20);
}catch(InvalidCharacterException e) {
    e.PrintStackTrace();
}

Should fix your issue. Feel free to respond if it does not and I will try and help you further.

  • This still produces an error. You cannot add a try-catch statement in a class body but outside of a method block. Also, welcome to Stack Overflow! – Nicholas Oct 19 '18 at 23:13
  • I think you need a class constructor, if you haven't got one you should convert the Foo class to the main class. Put the try/catch code in the main class constructor. – The Confraternity Oct 19 '18 at 23:51
  • Foo.bar is a static variable, so it should be able to be accessed without an actual instance of Foo, therefore a constructor should not need to exist to initialize bar. – Nicholas Oct 20 '18 at 00:11
  • Exactly what is it you are trying to do with this? I don't think Eclipse is very happy with having a potential Exception when class is created. You could always remove the Exception throw from the Bar class and it'd probably work. But if you really want the exception throw, you need to move the Bar object creation code from the top of the Foo class. – The Confraternity Oct 20 '18 at 00:21
  • Ok so I guess no other solutions? I'll just figure out how to work that out. – Nicholas Oct 20 '18 at 02:28
  • You could probably catch the exception in the Bar class in some way. – The Confraternity Oct 20 '18 at 09:37