Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java 8 lambda expresssions exception handling

Posted on 2016-11-14
3
Medium Priority
?
154 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 12

Accepted Solution

by:
Radek Baranowski earned 1000 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:
Suggested Courses

604 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