哪些编程语言没有运行时异常?
Which programming languages don't have runtime exceptions?
字符串名称=空;
name.ToLower();
在大多数语言中,这样的代码都可以编译。哪些语言会在编译时发现此类错误?
到目前为止我唯一知道的是榆树:http://elm-lang.org
Rust prevents a lot of errors at compile-time. Rust doesn't have runtime exceptions (other than panic!
使程序崩溃),而是使用 return 值进行错误处理。
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();
Rust 的核心概念之一是其他语言 (afaik) 所没有的 lifetimes,它可以防止在编译时出现悬空引用。然而,这是垃圾收集和引用计数语言不需要的东西。
Go 没有例外。与 Rust 一样,它使用 return 值来表示错误。但它不是空安全的:
// 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
以下语言 有异常处理,但可能仍然有助于回答问题。
Swift 也比平均安全,但它确实包含运行时异常。但是,Swift 的异常比 C++、Java、C# 等的异常更明确(例如,您必须在每次调用抛出函数前加上 try
,以及一个函数声明必须指定该函数是否可以抛出)。
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 是一种更安全的 JVM 语言,它也可以编译为 JavaScript。 Kotlin 也有例外。
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/, "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".
是默认情况下不允许变量为空的语言列表。
这使它们更安全,在编译时防止所有 NullPointerException
s,并且更清晰,因为您只需查看代码就知道在哪里允许传递 null,哪里不允许传递 null。
这些语言中的大多数还提供许多其他编译时安全功能。
字符串名称=空; name.ToLower();
在大多数语言中,这样的代码都可以编译。哪些语言会在编译时发现此类错误?
到目前为止我唯一知道的是榆树:http://elm-lang.org
Rust prevents a lot of errors at compile-time. Rust doesn't have runtime exceptions (other than panic!
使程序崩溃),而是使用 return 值进行错误处理。
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();
Rust 的核心概念之一是其他语言 (afaik) 所没有的 lifetimes,它可以防止在编译时出现悬空引用。然而,这是垃圾收集和引用计数语言不需要的东西。
Go 没有例外。与 Rust 一样,它使用 return 值来表示错误。但它不是空安全的:
// 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
以下语言 有异常处理,但可能仍然有助于回答问题。
Swift 也比平均安全,但它确实包含运行时异常。但是,Swift 的异常比 C++、Java、C# 等的异常更明确(例如,您必须在每次调用抛出函数前加上 try
,以及一个函数声明必须指定该函数是否可以抛出)。
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 是一种更安全的 JVM 语言,它也可以编译为 JavaScript。 Kotlin 也有例外。
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/, "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".
这使它们更安全,在编译时防止所有 NullPointerException
s,并且更清晰,因为您只需查看代码就知道在哪里允许传递 null,哪里不允许传递 null。
这些语言中的大多数还提供许多其他编译时安全功能。