Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

23
  • 17
    C has had a _Noreturn keyword since C11, but it's one of the few languages that do. Commented Aug 19, 2020 at 13:13
  • 8
    Kotlin also has the Nothing type, which can be used for exactly this.
    – Andy
    Commented Aug 19, 2020 at 13:37
  • 6
    At least with Kotlin's nothing and its most popular IDE, there's a static analysis immediately warning you when you invoke some code after calling a method with the Nothing return type, telling you that the code will never be reached. Unfortunately, this is not checked at compile time.
    – Andy
    Commented Aug 19, 2020 at 14:14
  • 5
    Nothing (in Kotlin) is actually much better then you guys say! Nothing represents a value that has 0 instances. Therefore, if a function returns Nothing, it must never return. There are two main standard library functions that return Nothing: exitProcess and throw (though, throw is not a function but a language intrinsic). This means that you can use throw in a function and the compiler can use it to do more complex flow analysis. For example, doing if(x == null) throw new Exc("x is null") can be replaced by if(x==null) error("x is null" assuming error returns Nothing. Commented Aug 19, 2020 at 21:34
  • 6
    Rust also has a Never type, denoted as ! for some obscure reason.
    – Thomas
    Commented Aug 20, 2020 at 12:44