Learning Java Lambdas by Unknown
Author:Unknown
Language: eng
Format: mobi, epub
Publisher: Packt Publishing
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.
TCP IP by Todd Lammle(2633)
MCSA Windows Server 2016 Study Guide: Exam 70-740 by William Panek(2309)
Networking A Beginner's Guide by Bruce Hallberg(1932)
The KCNA Book by Nigel Poulton(1923)
Red Hat Certified Specialist in Services Management and Automation EX358 Exam Guide by Eric McLeroy(1864)
31 Days Before Your CompTIA A+ Exams (Shanette Luellen's Library) by Benjamin Patrick Conry(1661)
MCSA Windows Server 2016 Study Guide: Exam 70-741 by William Panek(1477)
Unity Certified Programmer: Exam Guide by Philip Walker(1477)
PHP 7 Zend Certification Study Guide by Andrew Beak(1450)
Healthcare Information Security and Privacy (All-In-One) by Sean Murphy(1368)
CompTIA A+ Certification Guide (220-901 and 220-902) by Matthew Bennett(1337)
RHCSA & RHCE Red Hat Enterprise Linux 7: Training and Exam Preparation Guide (EX200 and EX300), Third Edition by Asghar Ghori(1302)
Essential Office 2016 by Wilson Kevin(1252)
Mobile Computing Deployment and Management by Robert J. Bartz(1252)
Microsoft Security, Compliance, and Identity Fundamentals Exam Ref SC-900 by Dwayne Natwick(1249)
Designing and Implementing Microsoft Azure Networking Solutions by David Okeyode(1241)
Essential Office 2016 (Computer Essentials) by Kevin Wilson(1224)
The Tao of Network Security Monitoring by Richard Bejtlich(1194)
CCNA Cisco Certified Network Associate Practice Labs and Simulations by Shwergho Smith(1144)