Solved

java 8 lambda expresssions exception handling

Posted on 2016-11-14
3
124 Views
Last Modified: 2016-11-29
Hi,
using java 8 lambda expresssions how to do exception handling. please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Accepted Solution

by:
Radek Baranowski earned 250 total points
ID: 41886782
Simple as it is, you can put the code using lambdas within try/catch block and work with expected exceptions like with any other ones. Here's simple example:

import java.util.ArrayList;

public class Lambda {
        
   ArrayList<Node> al;

   // inner class
	private class Node{ 
		double[] a;
		
	}
	
	public Lambda(){
		al = new ArrayList<Node>();
		al.add(new Node());
		al.add(new Node());
		al.add(new Node());
		al.add(new Node());
	}
	
	
	public static void main(String[] args) {
		
		Lambda l = new Lambda();
		try{				
			l.al.forEach( n ->
				System.out.println(n.a[2]*n.a[3])
				);
		} catch (java.lang.NullPointerException e) {
			System.out.println(e);
		}
		
		
	}

}

Open in new window

0
 
LVL 16

Assisted Solution

by:gurpsbassi
gurpsbassi earned 250 total points
ID: 41886786
I am assuming you are referring to Streams in Java 8?
They do not play well with exceptions. However with Optional types you have the ability to throw exceptions e.g. Optional.orElseThrow(...)


However guddi9 this is a VERY advanced topic for you. I would not recommend you learn this just yet until you are fluent in java up to version 7.
This means being able to decipher if-else control logic without the need for venn diagrams.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41896405
package syout;
import java.util.ArrayList;

public class Lambda {
        
   ArrayList<Node> al;

   // inner class
	private class Node{ 
		double[] a;
		
	}
	
	public Lambda(){
		al = new ArrayList<Node>();
		al.add(new Node());
		al.add(new Node());
		al.add(new Node());
		al.add(new Node());
	}
	
	
	public static void main(String[] args) {
		
		Lambda l = new Lambda();
		try{				
			l.al.forEach( n ->
				System.out.println(n.a[2]*n.a[3])
				);
		} catch (java.lang.NullPointerException e) {
			System.out.println(e);
		}
		
		
	}

}

Open in new window


when i ran above code i got below exception.
please advise
java.lang.NullPointerException
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question