Which programming languages don't have runtime exceptions?
Asked Answered
I

3

13
string name = null;
name.ToLower();

In most languages such code would compile. Which languages would catch this and the like errors at compile time ?

The only one I know uptil now is elm: http://elm-lang.org

Infant answered 11/2, 2016 at 15:36 Comment(6)
Related: Are there languages without "null"?Roby
Are you interested in languages where string name = null is not allowed in the first place?Roby
@zenith I'm interested in languages which can catch as much errors as possible on compile time. So yeah I'm also interested in languages where objects can not be null.Infant
It seems that a better question title could be "Which languages catch as many errors as possible at compile-time?" in that case.Roby
@zenith I guess it's the same.You can't go much further in catching as many errors than to not have run-time exceptions. Compiler won't be able to check if your business logic is correct.Infant
@zenith could you post your link to languages without null as an answer? I would like to accept that :)Infant
R
0

Here's a list of languages that by default don't allow variables to be null.

This makes them a lot safer, preventing all NullPointerExceptions at compile-time, and clearer because you know where you're allowed to pass null and where not, simply by looking at the code.

Most of these languages also provide many other compile-time safety features.

Roby answered 20/2, 2016 at 12:53 Comment(0)
R
35

Rust prevents a lot of errors at compile-time. Rust doesn't have runtime exceptions (other than panic! which crashes the program), instead it uses return values for error handling.

let name = None; // error: mismatched types
let name: Option<String> = None; // ok

name.to_lowercase(); // error: no method named `to_lowercase` found for type `Option`

// correct:
match name {
    None => { /* handle None */ },
    Some(value) => { value.to_lowercase(); },
}

// or to ignore None and crash if name == None
name.unwrap().to_lowercase();

One of Rust's core concepts that no other language has (afaik) is lifetimes, which prevent dangling references at compile-time. However this is something that garbage-collected and reference-counted languages don't need.


Go doesn't have exceptions. Like Rust, it uses return values to signal errors. But it's not null-safe:

// strings can't be nil:
var name string = nil // error: cannot use nil as type string in assignment

// string pointers can:
var name *string = nil // ok

string.ToLower(*name) // panic: runtime error: invalid memory address

The following languages do have exception handling but might still be helpful to answer the question.


Swift is also safer than average, but it does include runtime exceptions. However, Swift's exceptions are more explicit than those of C++, Java, C#, etc. (e.g. you have to prefix each call to a throwing function with try, and a function declaration must specify whether that function may throw).

let name = nil // error: type of expression is ambiguous without more context
let name: String? = nil // ok

name.lowercaseString // error: value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?
name!.lowercaseString // will crash at runtime with: fatal error: unexpectedly found nil while unwrapping an Optional value
name?.lowercaseString // calls method only if name != nil

Kotlin is a safer JVM-language which also compiles to JavaScript. Kotlin also has exceptions.

val name = null
name.toLowerCase() // compile-error

if (name != null)
    name.toLowerCase() // ok

name?.toLowerCase() // call only if non-null

Ada is a language designed for safety-critical purposes. According to http://www.adaic.org/advantages/features-benefits/, its "many built-in checks allow the compiler or linker to detect errors that in a C-based language would only be caught during run-time".

Roby answered 11/2, 2016 at 17:6 Comment(6)
This answer is amazing.Municipalize
One thing to note is that you can have runtime exceptions in rust if you use unwrap(). Similar to swift's ! operator but much easier to search for and avoid.Highwrought
unwrap() in Rust would cause a panic, that is program termination, which is different from an exception. It also has the ? operator which has a similar effect.Nisus
I was going to ask why not Elm but then realised Elm wasn't even around when this was answered.Cassaundra
@Cassaundra Elm was around. I mentioned it in the question.Infant
@Infant D'oh! I can't even make the excuse that it was a particularly long question and it was hidden in there somewhereCassaundra
E
2

Elm, as the OP noted, is an excellent language without runtime exceptions. Another language that seems to be equally committed to safety is Pony.

Erin answered 9/8, 2022 at 14:4 Comment(0)
R
0

Here's a list of languages that by default don't allow variables to be null.

This makes them a lot safer, preventing all NullPointerExceptions at compile-time, and clearer because you know where you're allowed to pass null and where not, simply by looking at the code.

Most of these languages also provide many other compile-time safety features.

Roby answered 20/2, 2016 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.