5

For example:

FileOutputStream("file") 

would compile in Kotlin, but in Java it would give a compiler error. Why?

4
  • 5
    kotlinlang.org/docs/reference/… Commented Dec 9, 2017 at 22:31
  • 1
    Kotlin has no checked exceptions, as in C# and other languages Commented Dec 10, 2017 at 7:38
  • 1
    The aim of Kotlin creators is to make popular language. So they add a bunch of syntactical sugar and sacrifice everything that can confuse beginner. And checked exceptions often annoys beginners That's why. Commented Dec 10, 2017 at 11:29
  • Kotlin tries to get rid of boilerplate code whenever possible Commented Dec 11, 2017 at 2:57

2 Answers 2

4

Kotlin does away with Java's checked exceptions. Exceptions checked at compile time and declared in method signatures, though familiar to Java developers, are widely considered a failed experiment outside and to some degree inside the Java community.

So Kotlin did away with them, and with some of the boilerplate associated with using resources (like FileOutputStream) with the .use method shorthand for Java 7's try-with-resources.

Sign up to request clarification or add additional context in comments.

8 Comments

So what would be the appropriate way to handle those exceptions if i didn't know that they will occur in advance?
In the case of Java classes used in Kotlin, you can simply check the interface method signature or Javadoc. There may be some exceptions that you want to recover from and others that you want to let kill your thread. As mentioned above, FileOutputStream(File("path")).use{ ... } is most likely what you are looking for.
@NateVaughan If it's a Kotlin interface, it won't have any exceptions declared. And what if there is no Javadoc or it's incomplete?
@NateVaughan Then how am I supposed to recover from them if I don't know which ones I can get? This is so-called Pokemon exception handling (catch-em-all). Catch any and very helpfully say to the user that something is wrong, but I don't know what? That's precisely the problem with exceptions in Kotlin.
@NateVaughan Even discerning an IOException from Exception helps, there's nothing "thoughtful, detailed" in this. And exceptions can be missing from documentation for the same simple reason that the library authors don't have any compiler help when writing documentation either. Also I don't understand why this is supposed to happen only with the code you don't control. In my own code I often forget which methods can throw which exceptions either, and I'd rather have compiler tell me than writing all this stuff out by hand in comments (which you can also forget to check).
|
3

It can be difficult to answer without letting some opinions interfere. I will just say that Kotlin is aimed at large software projects and give you what the Kotlin team claims regarding checked exceptions (from https://kotlinlang.org/docs/reference/exceptions.html):

Checked Exceptions

Kotlin does not have checked exceptions. There are many reasons for this, but we will provide a simple example.

The following is an example interface of the JDK implemented by StringBuilder class:

Appendable append(CharSequence csq) throws IOException; What does this signature say? It says that every time I append a string to something (a StringBuilder, some kind of a log, a console, etc.) I have to catch those IOExceptions. Why? Because it might be performing IO (Writer also implements Appendable)… So it results into this kind of code all over the place:

try {
    log.append(message)
}
catch (IOException e) {
    // Must be safe
}

And this is no good, see Effective Java, Item 65: Don't ignore exceptions.

Bruce Eckel says in Does Java need Checked Exceptions?:

Examination of small programs leads to the conclusion that requiring exception specifications could both enhance developer productivity and enhance code quality, but experience with large software projects suggests a different result – decreased productivity and little or no increase in code quality.

Other citations of this sort:

Java's checked exceptions were a mistake (Rod Waldhoff)

The Trouble with Checked Exceptions (Anders Hejlsberg)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.