简体   繁体   中英

How to catch the exception thrown from a lambda

I'm probably missing something obvious here, but how do I catch the IOException that I rethrow from the lambda?

   Future<Response<R>> futureResp = pool.submit(() -> {
        Response<R> res;
        try {
            res = call.execute();
        } catch (IOException e) {
            throw new IOException(e);
        }
        return res;
    });

The entire method would look like this:

@Override
public RetrofitResponse<R> adapt(Call<R> call) {
    if (networkUtil.isOnline( ctx )) {
        ExecutorService pool = Executors.newFixedThreadPool(1);

        Future<Response<R>> futureResp = pool.submit(() -> {
            Response<R> res;
            try {
                res = call.execute();
            } catch (IOException e) {
                throw new IOException(e);
            }
            return res;
        });

        Response<R> resp;
        try {
            resp = futureResp.get();
        } catch (Exception e) {
            e.printStackTrace();
            return new RetrofitResponse<>( ErrorStatus.PROCESSING_ERROR, e.getMessage() );
        }
        if (resp != null) {
            return new RetrofitResponse<>( new ApiResponse<>(resp) );
        } else {
            return new RetrofitResponse<>( ErrorStatus.PROCESSING_ERROR, "Response is null" );
        }
    } else {
        return new RetrofitResponse<>( ErrorStatus.NETWORK_ERROR, "No internet network detected!" );
    }

}

I basically want to wrap errors in a custom object that keeps state about possible errors that are not necessarily related to either lack of network connection or http errors.

Warpping this block in another try-catch block doesn't work since the ide indicates that there is no IOException to be caught.

This has nothing to do with lambda expressions at all. Therefore, you don't need to catch and re-throw the exception “from the lambda”; you can simply submit your job like

Future<Response<R>> futureResp = pool.submit(() -> call.execute());

The key point is that this will invoke submit(Callable) and the Callable interface allows throwing checked exceptions. And Future.get() states:

Throws:


ExecutionException - if the computation threw an exception

So when you catch(ExecutionException) at the get() call, its cause will be the thrown IOException , if any.

You need to catch the original Exception and throw it as UncheckedException maybe as your own that extends beforementioned and holds the original exception as its cause.

Then catch this UncheckedException and if needed dig the cause from it.

You need to rethrown your error as a UncheckedException

  catch(IOException e) {
      throw new RuntimeException(e);
  }

Or extend RuntimeException and throw a custom one.

You will get the error in the future.get()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM