Java 8 Lambda function that throws exception?

后端 未结 26 2166
臣服心动
臣服心动 2020-11-22 03:14

I know how to create a reference to a method that has a String parameter and returns an int, it\'s:

Function         


        
26条回答
  •  梦谈多话
    2020-11-22 03:31

    I will do something generic:

    public interface Lambda {
    
        @FunctionalInterface
        public interface CheckedFunction {
    
            T get() throws Exception;
        }
    
        public static  T handle(CheckedFunction supplier) {
            try {
                return supplier.get();
            } catch (Exception exception) {
                throw new RuntimeException(exception);
    
            }
        }
    }
    

    usage:

     Lambda.handle(() -> method());
    

提交回复
热议问题