Solved

java 8 lambda expresssions exception handling

Posted on 2016-11-14
3
88 Views
Last Modified: 2016-11-29
Hi,
using java 8 lambda expresssions how to do exception handling. please advise
0
Comment
Question by:gudii9
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 15

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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