Chapter: Java The Complete Reference - The Java Language - Lambda Expressions

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Lambda Expressions and Exceptions

A lambda expression can throw an exception. However, it if throws a checked exception, then that exception must be compatible with the exception(s) listed in the throws clause of the abstract method in the functional interface.

Lambda Expressions and Exceptions

 

A lambda expression can throw an exception. However, it if throws a checked exception, then that exception must be compatible with the exception(s) listed in the throws clause of the abstract method in the functional interface. Here is an example that illustrates this fact. It computes the average of an array of double values. If a zero-length array is passed, however, it throws the custom exception EmptyArrayException. As the example shows, this exception is listed in the throws clause of func( ) declared inside the DoubleNumericArrayFunc functional interface.

 

 

// Throw an exception from a lambda expression.

 

interface DoubleNumericArrayFunc {

 

double func(double[] n) throws EmptyArrayException;

 

}

 

class EmptyArrayException extends Exception { EmptyArrayException() {

 

super("Array Empty");

 

}

 

}

 

class LambdaExceptionDemo {

 

public static void main(String args[]) throws EmptyArrayException

 

{

 

double[] values       = { 1.0, 2.0, 3.0, 4.0 };

 

// This block lambda computes the average of an array of doubles.

DoubleNumericArrayFunc average = (n) -> {

 

double sum = 0;

 

if(n.length == 0)

 

throw new EmptyArrayException();

 

for(int i=0; i < n.length; i++) sum += n[i];

 

return sum / n.length;

 

};

 

System.out.println("The average is " + average.func(values));

 

// This causes an exception to be thrown.

 

System.out.println("The average is " + average.func(new double[0]));

 

}

 

}

 

The first call to average.func( ) returns the value 2.5. The second call, which passes a zero-length array, causes an EmptyArrayException to be thrown. Remember, the inclusion of the throws clause in func( ) is necessary. Without it, the program will not compile because the lambda expression will no longer be compatible with func( ).

This example demonstrates another important point about lambda expressions. Notice that the parameter specified by func( ) in the functional interface DoubleNumericArrayFunc is an array. However, the parameter to the lambda expression is simply n, rather than n[ ].

Remember, the type of a lambda expression parameter will be inferred from the target context. In this case, the target context is double[ ], thus the type of n will be double[ ]. It is not necessary (or legal) to specify it as n[ ]. It would be legal to explicitly declare it as double[ ] n, but doing so gains nothing in this case.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.