-1

I have a Project with two classes. One Object class and one GUI class. I would like to throw my own declared Exception if an error occurs. I have two methods:

public class getValueClass   {

private List<Value> liste;


public List<Value> getValues() {

    try {
        liste = this.getVal();
    } catch (ValueException ex) {
        System.out.println("EXCEPTION!! " + ex.getMessage());
        ex.printStackTrace();
    } 
    return liste;
}


public List<Value> getVal() throws  ValueException
{
liste = null;
try {

    // initialize list
    // do some stuff


    //test exception
    if(1 == 1)
    {
        throw new Exception();  
    }


} catch (Exception ex) {
    throw new ValueException("QWE " + ex);
} 
return liste;

}
}

Now the exception is thrown and I catch the exception in my getValues Method and print the Message/Stack

But I call the Method getValues in my GUI-Class and I want to catch the Exception there and print some Information in my dialog!

GUI:

    public void myMethod()
{
try
            {

            l = cs.getValues();

            }
            catch(Exception ex)
            {
                System.out.println("TEST " + ex.getMessage());
            }
            }

But I don't get there because I already catch it in the getValues() method.

Is it possible to make it like this WITHOUT adding throws at method declaration for getValues() method? ( I get this method from an interface and will not change it)

Michael
  • 251
  • 1
  • 2
  • 13
  • Exceptions that inherit from `RuntimeException` can be thrown without being declared in method signatures. That said, I consider undeclared exceptions bad practice – Siguza Jul 20 '16 at 18:20
  • @Siguza phew! I thought I was alone in that opinion :) – Jeeter Jul 20 '16 at 18:27
  • 1
    http://stackoverflow.com/questions/27578/when-to-choose-checked-and-unchecked-exceptions – jaco0646 Jul 20 '16 at 18:41

2 Answers2

2

You could throw an unchecked RuntimeException such as IllegalArgumentException or customized RuntimeException subclass.

public List<Value> getValues() {
    try {
        liste = this.getVal();
    } catch (ValueException ex) {
        throw new IllegalArgumentException(ex.getMessage(), ex);
    } 
    return liste;
}
Whome
  • 10,181
  • 6
  • 53
  • 65
1

There is a way to do what you want, but I advise against it depending on your intended purpose of ValueException, as it could be the source of future bugs

You can have ValueException extend RuntimeException. The RuntimeException set of exceptions, as the name implies, are thrown at runtime and are not declared at compile time, and need to be explicitly caught. This way you wouldn't have to add a throws declaration to the getValues() method, but would still catch it in your main method.


Disclaimer explained:

The reason I am not a fan of this idea (and RuntimeExceptions in general) is because they're uncaught until explicitly looked for. This in my mind doesn't make for easy-to-use code, and while it has it's very handy uses, I don't feel right using them because of the uncertainty they carry

Again, this is my opinion, not Java's

Jeeter
  • 5,887
  • 6
  • 44
  • 67