Module 2 Milestone 2 InvalidInputDate

Build fails in InvalidInputDate. I read a few threads and understood why the build failed. but idk how to resolve it. i tried using try and catch block but after that the output that i got was not correct. Can someone give some hints or some useful links to refer to?

Check it here.

Try checking if the input date is more than the end date and throwing some error if that is the case.

I read this thread. But if i try implementing a bit using if, else try and catch the output becomes wrong.

don’t use try catch block. you can use throw keyword and throw the exception you want.

How should i throw this error? Through if else or should i include the Exception in throws while declaring the function?

try using “if” with some condition including dates

check the format of the date you’re inputting. Also check if the date itself is correct. read the todos to know what the start and end date should be

Hey Maybe there is testcase which is checking for that enddate is greater then startdate so be careful about that add a checking statement for that

Just see the test file , it need an instance of RuntimeException to be thrown when invalid dates condition is met. Just throw the exception on invalid dates case.
In java we can throw an exception inside if block like this :
if (Condition) {
thrown new ExceptionName();
}
Hope this link helps : https://beginnersbook.com/2013/04/throw-in-java/
Also read this : https://courses.cs.washington.edu/courses/cse341/98au/java/jdk1.2beta4/docs/api/java/lang/RuntimeException.html
https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/
@Aanand

To avoid this problem without using the try-catch/ throw use this:-
https://stackoverflow.com/questions/27005861/calculate-days-between-two-dates-in-java-8
and also if you are facing 0 year difference:-
https://forum.crio.do/t/annualized-return-when-time-interval-is-0-years/7481

1 Like

Thank you so much everyone! Found the way :smiley:

How did you solve the problem?
I am throwing runtime exception if dates are invalid but the test is still failing

Has the issue been solved? Did the Build fail or did the assessment fail?

1 Like

The problem was solved.

1 Like