0

I have a requirement of exception handling for all methods, suppose that I have a project and within the project, the StaleStateException is thrown, and I need to handle it, I want to do it like this:

class Util() {
    public static void handle(XXXX method) {
        try{
            //invoke method
        } catch(StaleStateException e) {
            //do something
        }
    }
}

How can I implement this method?

rosaqq
  • 426
  • 4
  • 16
zhianABCD
  • 223
  • 1
  • 4
  • 15
  • 1
    does [this](https://stackoverflow.com/questions/4685563/how-to-pass-a-function-as-a-parameter-in-java/21681010) help you? – amsjntz Jun 21 '21 at 09:10
  • If you want to handle any method, then you probably want to use generic methods. – David Lee Jun 21 '21 at 09:14
  • @DavidLee, Yes correct, I want to handle any methods and I want any of the method can be the parameter passed to this function – zhianABCD Jun 21 '21 at 09:16
  • Do you need to pass arguments to the method and are there return values? Or is the signature always `void doSomething()`? – Matt Jun 21 '21 at 09:20
  • @Matt, the method passed to this util method can be method with any argument and return type. – zhianABCD Jun 21 '21 at 09:22
  • How do you expect to pass a method to `handle(...)` ? Reflection? The solution might be more confusing that just wrapping everything with try-catch... – rosaqq Jun 21 '21 at 09:38
  • check this https://stackoverflow.com/questions/2186931/java-pass-method-as-parameter – Ashish Mishra Jun 21 '21 at 09:49
  • Define a function to accept a function? That would be `Function, ?> function`. – MC Emperor Jun 21 '21 at 10:05

2 Answers2

0
Function<InputType, Void> = input -> {
    // do stuff
    System.out.println(input.toString());
}

class Util() {
    public static void handle(Function<InputType, Void> method) {
        try{
            input.apply(input);
        } catch(StaleStateException e) {
            //do something
        }
    }
}
SoT
  • 898
  • 1
  • 15
  • 36
0

Here is another way to do this:

public class Methods {
  public static void someMethodToPass(String s){
    System.out.println("Invoked, Here is the argument: " + s);
  }

  public static void invokerMethod(Method m) throws InvocationTargetException, IllegalAccessException {
    m.invoke(null,"Some argument");
  }

  public static void main(String[] args) {
    try {
        invokerMethod(Arrays.stream(Methods.class.getMethods()).filter(m -> m.getName().equals("someMethodToPass")).findFirst().get());
    } catch (InvocationTargetException | IllegalAccessException e) {
        e.printStackTrace();
    }
  }
}
Roophie
  • 141
  • 8
  • Why not `Methods.class.getDeclaredMethod("someMethodToPass", String.class)` instead of `Arrays.stream(Methods.class.getMethods()).filter(m -> m.getName().equals("someMethodToPass")).findFirst().get()` ? Also, this is what I meant in my comment on the question. OP wanted to handle one type of exception with ease and now has a new method that throws 2 other exceptions he has to handle... – rosaqq Jun 21 '21 at 09:48