Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which programming languages don't have runtime exceptions?

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

like image 362
Tomas Avatar asked Feb 11 '16 15:02

Tomas


People also ask

Which programing language has not facility for exception handling?

Many modern languages provide rich exception handling features, but Apple's Swift programming language does not provide an exception handling mechanism.

Can we write runtime exception in Java?

yes you can. You can throw anything that extends Throwable - check the javadocs. docs.oracle.com/javase/6/docs/api/java/lang/… Yes, you can throw a RuntimeException.

How can you avoid the runtime exception?

To avoid the Java RuntimeException completely, declare the array as the least generic type, which in this case would be Double. The Java RuntimeException is only one of many different types of bugs that can cause a software program to fail.

Does C have exception handling?

C does not provide direct support for error handling (also known as exception handling). By convention, the programmer is expected to prevent errors from occurring in the first place, and test return values from functions.


1 Answers

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".

like image 107
emlai Avatar answered Oct 23 '22 05:10

emlai