Why am i getting the “Syntax error on token(s), misplaced construct(s)” error with Supplier?

I get the error with the following code:

private Supplier<ProfileData > expectedValidProfileData = () -> {
    try {
        ProfileData profileData = new ProfileData ();
        return profileData ;
    } catch (Exception ex) {
    }
    return null;
};

I’m not sure why though, because if I copied the code directly from another file where the error isn’t present

I noticed that I don’t get the error if I remove the try-catch statements

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

add return statement in catch part (exception as per need) that’s why it is working without try-catch block

private Supplier<ProfileData > expectedValidProfileData = () -> {
    try {
        ProfileData profileData = new ProfileData ();
        return profileData ;
    } catch (Exception ex) {
         //add return statement here
         return null;
    }
};