Learning Java Lambdas by Unknown

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



Copyright Disclaimer:
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.