0

I have a class that extends another class (In this case, it's an Exception):

public class NewTypeException extends Exception {
    private String exceptionField;

    public String getExceptionField() {
        return exceptionField;
    }

    public void setExceptionField(String exceptionField) {
        this.exceptionField = exceptionField;
    }
    public NewTypeException(String cause, String reason) {
        super(cause);
        exceptionField = reason;
    }
}

And another class (for sake of example, lets call this PrintUtil) that has two methods with similar signatures, the only difference being the Exception type changes to the subclass:

void doStuff(Exception ex) {
    System.out.println(ex.getMessage());
}

void doStuff(NewTypeException ex) {
    System.out.println("New Type Exception");
    System.out.println(ex.getExceptionField());
    System.out.println(ex.getMessage());
}

In a lot of places in my code, I have a bunch of

try {
    // code
} catch (Exception ex) {
    printUtil.doStuff(ex);
}

After adding this new exception type, I want to have this line call the most specific method it can, depending on the argument. However, it seems when I test this, this will only use the method for Exception even if the runtime type fits another method (e.g. NewTypeException). Is there any way to do this other than replace hundreds of sections of

try {
    // code
} catch (Exception ex) {
    printUtil.doStuff(ex);
}

with

try {
    // code
} catch (NewTypeException ex) {
    printUtil.doStuff(ex);
} catch (Exception ex) {
    printUtil.doStuff(ex);
}

? This seems like something really basic a OOP language should be able to do...

uberhaxed
  • 225
  • 1
  • 9
  • What does `printUtil#doStuff` do? – RaminS Mar 04 '19 at 18:37
  • The second code block shows the implementation of `PrintUtil#doStuff(Exception ex)` and `PrintUtil#doStuff(NewTypeException ex)`. – uberhaxed Mar 04 '19 at 18:38
  • When you do `catch (Exception ex)`, the compilation of the static dispatch is on the type `Exception`. Underneath, if in `printUtil.doStuff(ex);` you did a print on the .class(), or an `instanceof`, it would be a `NewTypeException`. So, you can make `printUtils.doStuff(...)` vary based upon the type it gets, or update the `catch` blocks. The underlying concept -- which very much is OOP -- is discussed a bit in [static vs. dynamic binding](https://stackoverflow.com/questions/19017258/static-vs-dynamic-binding-in-java). – KevinO Mar 04 '19 at 18:42

1 Answers1

0

Not possible the way you're doing it. Method calls check the parameters arguments at compile time taking into account the declared types.

You may move the exception handling code somewhere else, but either you would need to instanceof or catch specific exceptions:

try {
    // code
} catch (Exception ex) {
    printUtil.handleExceptions(ex);
}

With utility class:

class PrintUtil {
    public static handleExceptions(Exception e) {
        try {
            throw e;
        } catch (NewTypeException ex) {
            doStuff(ex);
        } catch (AnotherTypeException ex) {
            doStuff(ex);
        }
    }
    ...
}
Klaimmore
  • 680
  • 1
  • 6
  • 16