考虑以下用于写入文件的功能:
该方法背后的想法是,以允许用户在不同的实施方式中通过InputStream
的方法,以便writeToFile
可以被称为例如用GZIPOuputStream , SnappyOuputStream
(快速压缩)或简单的FileInputStream
。
1
2
3
4
5
6
7private static void writeToFile(File file, String value, Function<OutputStream, OutputStream> writing) throws IOException{ try (PrintWriter pw = new PrintWriter(new BufferedOutputStream (writing.apply(new FileOutputStream(file))))) { pw.write(value); } }
这是一个整洁的函数,可以这样调用:
1
2
3
4
5
6
7
8
9
10
11public static void main(String[] args) { try { //Write with compression //DOES NOT COMPILE!! writeToFile(new File("test"), "Hello World", GZIPOutputStream::new); //Just use the FileOutputStream writeToFile(new File("test"), "Hello World", i->i); }catch(IOException e){ //deal with exception as you choose } }
不幸的是,正如评论中指出的那样,它无法编译! 它之所以无法编译,是因为GZIPOutputStream
在其构造函数中引发了IOException
。 如果将IOException从lambda中抛出,然后可以在try catch块中进行处理,那将是很好的选择–但这不是lambda的工作方式:-(
实际上,这是您必须处理异常以使代码得以编译的方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public static void main(String[] args) { try { //Write with compression //COMPILES BUT SO UGLY writeToFile(new File("test"), "Hello World", i -> { try { return new GZIPOutputStream(i); } catch (IOException e) { //HOW ARE WE SUPPOSED TO DEAL WITH THIS EXCEPTION?? throw new AssertionError(e); } }); //Just use the FileOutputStream writeToFile(new File("test"), "Hello World", i->i); }catch(IOException e){ //deal with exception as you choose } }
这不仅难看,而且还给您带来了如何处理IOException的尴尬问题。 在这种情况下,我们刚刚将其重新包装在AssertionError中。 有关处理这种情况的正确方法,请参阅我以前的文章 “使用异常作弊”。
但是有解决此问题的方法。 可以使用创建带有值的自定义函数来返回值并抛出Exception ,而不是使用带有值并返回值的java.util.function.Function
。 这样, writeToFile
的客户端代码writeToFile
干净了,并且可以自然地处理异常。 而且,lambda现在以它们使我们的代码更漂亮和更易于理解的方式使用。
请参阅下面的完整代码清单:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30package util; import java.io.*; import java.util.zip.GZIPOutputStream; public class LambdaExceptions { public static void main(String[] args) { try { //Write with compression writeToFile(new File("test"), "Hello World", GZIPOutputStream::new); //Just use the FileOutputStream writeToFile(new File("test"), "Hello World", i->i); }catch(IOException e){ //deal with exception as you choose } } private static void writeToFile(File file, String value, ThrowingFunction<OutputStream, OutputStream, IOException> writing) throws IOException{ try (PrintWriter pw = new PrintWriter(new BufferedOutputStream (writing.apply(new FileOutputStream(file))))) { pw.write(value); } } @FunctionalInterface public interface ThrowingFunction<I, O, T extends Throwable> { O apply(I i) throws T; } }
翻译自: https://www.javacodegeeks.com/2015/05/exceptions-in-lambdas-an-elegant-solution-to-a-bit-of-a-mess.html
最后
以上就是大胆学姐最近收集整理的关于Lambdas中的例外:有点混乱的优雅解决方案的全部内容,更多相关Lambdas中内容请搜索靠谱客的其他文章。
发表评论 取消回复