Solved

java 8 lambda expresssions exception handling

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now