Unable to throw exception for invalid dates

I am confused regarding which exception to throw on Invalid Dates and Invalid Stocks. I tried accessing relevant test file classes as exception using instance of that class. I also, tried using .orElse() function with Stream but don’t know what exception to show.
Please help as I am stuck on this issue for last 2 days.

@hiteshkumar

Will you please elaborate what is happening with you in a bit detail ?

I am getting expected output for valid dates. But for invalid dates I am getting error. So I need to throw exception or I need to print latest valid date values? Also, in both cases get result I am not able to understand.

which milestone are you talking about, also can you please give me the detail about in which file the said todo is written ?

milestone 2 module 2, file name - PortfolioManagerApplication.java, TODO - Test the function using gradle commands below
./gradlew run --args=“trades.json 2020-01-01”
getting error on this date. And don’t know how to go about it.

@hiteshkumar

You can do the following stuff to get rid of it

1: Try to enclose the getforobject method with try and Catch block, and catch following exceptions: JsonGenerationException , IOException and JsonMappingException

2: As you are saying there is no data available for the provided date, then you can able to do one thing, try to keep a list and add all results into it, if its empty after all iterations then you can trigger it as Invalid date and throw throw new RuntimeException()

Try these, if its not working ping me, I’ll again take a look into it :slight_smile:

When we throw exception, is the build suppose to fail. Like I am throwing some runtime exception then the build fails and shows that there was a runtime exception.

@hiteshkumar If you are throwing some exceptions in the function then you need to add those exceptions as throws in the function definition.

I suggest you add all those exceptions in the function definition with throws keyword.

I added throw new RuntimeException() in the catch block and got error shown in previous image. And when I added print statement in catch block api calls were successfully made but still build failed as in below image

@hiteshkumar can you DM me with the screenshot of what exactly you’re doing with the code ?

@hiteshkumar

I got the point of what is happening with your code.

See you’re catching Exception e which is parent class of all the Exceptions, but you’re not adding it in throws.

Also you should need to be specific about the exceptions and need to add them only rather then adding Exception as the only one.

I want to repeat the earlier written stuff once again.

You can do the following stuff to get rid of it

1: Try to enclose the getforobject method with try and Catch block , and catch following exceptions: JsonGenerationException , IOException and JsonMappingException

and after that check, if the list is empty or not, and throw a new Runtime exception accordingly.

@VKW1111 has given a good answer, can you let us know if your issue is resolved or not?

understood the approach will try it after the foundation series as I am weak in Exception Handling :sweat_smile:. Will let you know if I resolve the issue.

@hiteshkumar

Feel free to resolve it in session, and also you can always find good tutorials of the concept on youtube. It will not gonna take your much time, all the best :slight_smile:

1 Like

@hiteshkumar Is your issue resolved or not?

I’m closing this topic due to no response from anyone since last 12 hours. If your query is resolved you can mark any reply as a solution. If you still have the query, please create a new topic paste the link of this topic in the description.

Closing this topic as there is no response since last 12 hours. Please feel free to create a new topic and post this question as a reference link in the description of the new topic.