简体   繁体   English

是否有可以容忍运行时错误的编程语言?

[英]Is there programming language which can tolerate runtime errors?

Is there a programming language which can consume the following input: 是否有一种编程语言可以使用以下输入:

m = 1;
n = 2/0;
print(n);
print(m);

and successfully print "1" on the screen? 并在屏幕上成功打印“1”?

Maturity of that language and quality of implementation doesn't matter much. 该语言的成熟度和实施质量无关紧要。

EDIT: Don't take question explanation literally. 编辑:不要从字面上解释问题。 I'm not interested in division by 0. I try to find a language which is insensitive to (almost) all runtime errors. 我对0除以后并不感兴趣。我试图找到一种对(几乎)所有运行时错误都不敏感的语言。

Visual Basic: On Error Resume Next Visual Basic: On Error Resume Next

And I'd like to point out that most languages can handle the above with whatever keywords the languages allow for hooking into interrupts. 我想指出的是,大多数语言都可以使用语言允许连接到中断的任何关键字来处理上述内容。

[ EDIT ] [编辑]

Okay, after OP's edit, it seems I completely misunderstood the question. 好的,在OP的编辑之后,似乎我完全误解了这个问题。 Nevertheless I am still leaving my answer here as someone might get some new information from it and anyway deleting it would serve little purpose. 然而,我仍然在这里留下我的答案,因为有人可能会从中获得一些新的信息,无论如何删除它都没什么用处。


Take a lazy language like Haskell. 采取像Haskell这样的懒惰语言。 Define print so that it tries to print the value ignoring any error that occurs while printing. 定义print以便它tries打印该值,忽略打印时发生的任何错误。 And there you have it, a language that behaves as described in your question. 而且你有它,一种行为与你的问题中描述的一样的语言。

Code example in Scala: Scala中的代码示例:

Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import util.control.Exception._
import util.control.Exception._

scala> def print[A](x: => A) {
     |   ignoring(classOf[Exception]) {
     |     println(x)
     |   }
     | }
print: [A](x: => A)Unit

scala> lazy val m = 1
m: Int = <lazy>

scala> lazy val n = 2 / 0
n: Int = <lazy>

scala> print(n)

scala> print(m)
1

(Note: Scala is not a lazy language by default, but supports lazy semantics optionally) (注意:默认情况下,Scala不是一种惰性语言,但可选择支持惰性语义)

In Mathematica you don't need an error catching command 在Mathematica中,您不需要错误捕获命令

Pgm: PGM:

Off[General::infy] (*Turn off infinity error messages*)  
m = 1;  
n = 2/0;    
Print[n];     
Print[m];  

Output: 输出:

ComplexInfinity
1

If you omit the first line (the error suppressing command), and additional warning message is printed: 如果省略第一行(错误抑制命令),则会打印其他警告消息:

Power::infy: Infinite expression 1/0 encountered. >>

Moreover, you can operate with the "ComplexInfinity" value of n: 此外,您可以使用n的“ComplexInfinity”值进行操作:

 Print[1/n]  

gives

 0

Any language that uses IEEE 754 floating point arithmetic. 任何使用IEEE 754浮点运算的语言。 Divided by zero is Infinity. 除以零是无限。

For example in Javascript: 例如在Javascript中:

> 1/0
Infinity

Dependently typed programming languages such as Idris, Agda or Coq will not compile if you ever try to do an illegal action. 如果您尝试执行非法操作,依赖类型的编程语言(如Idris,Agda或Coq)将无法编译。 Here is a safe example of your code in Idris. 以下是Idris中代码的安全示例。

safeDivide : Nat -> (y:Nat) -> so (y /= 0) -> Nat
safeDivide x y p = div x y

main : IO ()
main = 
  print (show 1)   -- compiles successfully
  print (show (safeDivide 2 1 oh))   -- compiles successfully
  -- print (show (safeDivide 2 0 oh))   -- throws an error at compile time

Dependently typed languages allow you to write proofs so that their type system can verify that your code will work the way it should. 依赖类型语言允许您编写校样,以便其类型系统可以验证您的代码将按照应有的方式工作。 By defining safeDivide with a proof ( so (y /= 0 ), you're guaranteeing that your program won't even compile if 0 is ever propagated to this function. 通过使用证明定义safeDivideso (y /= 0 ),您可以保证如果0传播到此函数,您的程序甚至不会编译。

很多年前,我们在学校的COBOL老师曾经说'COBOL是唯一允许除零的编程语言'(没有给出运行时错误)。

Bourne Shell: Bourne Shell:

M=1
N=expr 2 / 0
echo $N
echo $M

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM