K
K
kidar22015-12-22 08:04:09
Java
kidar2, 2015-12-22 08:04:09

How to bypass compilation error in lambda expression?

There is this simple code:

final StringBuilder p = new StringBuilder();
List<Stirng> arr = ....;
arr.forEach(m -> p.append("names[]=" + URLEncoder.encode(m, "UTF-8")));

On the second line, the compiler complains with the following error:
Error:(172, 78) java: unreported exception java.io.UnsupportedEncodingException; must be caught or declared to be thrown
The current method has the possibility of all types of errors: throws Exception.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2015-12-22
@kidar2

wrap
p.append("names[]=" + URLEncoder.encode(m, "UTF-8"));
in try\catch

J
jackroll, 2015-12-22
@jackroll

List<Stirng> arr = ....;
     ^^^^^

I guess it should be "String" here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question