• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

CyclicBarrier - Exception

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dears,

I have been trying to run the below program, however i am getting an error stating

LogHelper.java:41: error: unreported exception InterruptedException; must be caught or declared to be thrown
service.submit(() -> log.performTask(c1,c2));}

But i have declared this exception in the signature of the main method. But still facing this issue. Can someone please explain  me  whats wrong



 
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This lambda can throw InterruptedException, or at least the performTask method can, and it is not part of the main method -- it will be executed elsewhere, by some component associated with the ExecutorService. But the exception must be caught at the place where the code is executed. So you need to wrap log.performTask in something which will catch the exception and deal with it, inside the lambda.
 
Priyanka Leo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that the lambda is throwing the exception. SO how can we handle the exception in such case?
I am not aware about handling exception with Lambda
 
Paul Clapham
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
log.performTask(c1,c2) can throw an exception, so you need to catch that exception. I suggest that you already know how to write code to catch that exception...

In other words, don't make it harder for yourself by thinking of your task as "handling an exception with lambda". The task is simply "handling an exception", which you already know how to do.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic