Learning Java Lambdas by Weston Toby
Author:Weston, Toby [Weston, Toby]
Language: eng
Format: azw3, epub
Publisher: Packt Publishing
Published: 2017-03-31T04:00:00+00:00
Exception handling
There's no new syntax for exception handling in lambdas. Exceptions thrown in a lambda are propagated to the caller, just as you'd expect with a regular method call. There's nothing special about calling lambdas or handling their exceptions.
However, there are some subtleties that you need to be aware of. Firstly, as a caller of a lambda, you are potentially unaware of what exceptions might be thrown, if any and secondly, as the author of a lambda, you're potentially unaware what context your lambda will be run in.
When you create a lambda, you typically give up responsibility of how that lambda will be executed to the method that you pass it to. For all you know, your lambda may be run in parallel or at some point in the future and so any exception you throw may not get handled as you might expect. You can't rely on exception handling as a way to control your program's flow.
To demonstrate this, lets write some code to call two things, one after the other. We'll use Runnable as a convenient lambda type.
public static void runInSequence(Runnable first, Runnable second) { first.run(); second.run(); }
If the first call to run were to throw an exception, the method would terminate and the second method would never be called. The caller is left to deal the exception. If we use this method to transfer money between two bank accounts, we might write two lambdas. One for the debit action and one for the credit.
public void transfer(BankAccount a, BankAccount b, Integer amount) { Runnable debit = () -> a.debit(amount); Runnable credit = () -> b.credit(amount); }
we could then call our runInSequence method like this:
public void transfer(BankAccount a, BankAccount b, Integer amount) { Runnable debit = () -> a.debit(amount); Runnable credit = () -> b.credit(amount); runInSequence(debit, credit); }
any exceptions could be caught and dealt with by using a try/catch like this:
public void transfer(BankAccount a, BankAccount b, Integer amount) { Runnable debit = () -> a.debit(amount); Runnable credit = () -> b.credit(amount); try { runInSequence(debit, credit); } catch (Exception e) { // check account balances and rollback } }
Here's the thing. As an author of the lambdas, I potentially have no idea how runInSequence is implemented. It may well be implemented to run asynchronously like this:
public static void runInSequence(Runnable first, Runnable second) { new Thread(() -> { first.run(); second.run(); }).start(); }
In which case any exception in the first call would terminate the thread, the exception would disappear to the default exception handler and our original client code wouldn't get the chance to deal with the exception.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12569)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7056)
Microservices with Go by Alexander Shuiskov(6819)
Practical Design Patterns for Java Developers by Miroslav Wengner(6736)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6677)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6083)
The Art of Crafting User Stories by The Art of Crafting User Stories(5609)
NetSuite for Consultants - Second Edition by Peter Ries(5549)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5350)
Kotlin in Action by Dmitry Jemerov(5062)
