In Java, Android Studio gives Unhandled Exception error for some functions. for e.g., when we write code for converting a String to a Date variable, it shows Unhandled Exception:ParseException & pressing Alt+Insert automatically adds the try catch block & we can write code in catch block for exception conditions. But in Kotlin I am not seeing such warnings. Is there any other way to handle exceptions in Kotlin?
Asked
Active
Viewed 517 times
1
-
2The exceptions that force you to handle them in Java are called checked exceptions. You can still catch them in Kotlin but it is not enforced. This doesn’t mean you don’t need to handle them. It just saves you from having to declare it in every method signature between the IO code and the top level UI code where you would show the user an error if there was a problem. A common pattern in Kotlin however is to intercept and use a result wrapper at a low level or just make null represent a failure. – Tenfour04 Nov 10 '20 at 05:38
-
I seems you are using `SimpleDateFormat` for parsing into `Date`. I recommend you don’t. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `DateTimeFormatter` and other appropriate classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 10 '20 at 09:14