E
E
Evgeny Kornachev2017-07-26 22:50:47
Java
Evgeny Kornachev, 2017-07-26 22:50:47

How to correctly and painlessly refuse CHECKED EXCEPTION (java-> kotlin transition)?

I write and wrote mainly in java, I'm used to using checked exceptions. I'm used to the IDE forcing me to handle exceptions if I forgot to do it myself. At the same time, it is possible to handle different types of exceptions differently, and ide itself reminds me of their types (combining x, etc.).
I decided to try kotlin and here it is for you ... It turns out that all exceptions are unchecked there. Actually for me it's wild.
1) How can I find out that the method can throw something and that it is worth wrapping the code in a try block?
2) How to handle a different type of exception differently? After all, then it is necessary to know which exception (maybe even several) the method throws, but this is not written in the signature. Digging into the code? Brrr...
3) How to auto-refactor? for example, if the operation of the method has changed and no longer throws one of the exceptions, then dead code will remain. Or worse, a new exception has been added. Search all places where the method is used and introduce a new catch?
4) Or can wrap everything and everywhere for every fireman (even if the method does not throw anything)? But it's kind of annoying and disgusting.
How to proceed? The same is written in C# and other languages ​​with unchecked exceptions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-07-27
@zelan

Write tests.
Checked Esception, as practice shows, only interfere.
If the code is not yours, but a library code, for example, read the library API documentation.
The project I personally work on has no problems with checked exceptions. If we know for sure that a kotlin method is called from Java code and throws a checked exception, then such a method is marked with the @Throws annotation indicating the type Exception-a. If we know for sure that the Java code called from the Kotlin code throws a checked exception, and it is important for us to catch it, then we wrap the code in a try\catch.
It doesn't seem to be difficult...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question