Solved

java 8 lambda expresssions exception handling

Posted on 2016-11-14
3
99 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

790 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