Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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.

like image 435
DSblizzard Avatar asked Oct 31 '10 04:10

DSblizzard


2 Answers

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.

like image 114
ta.speot.is Avatar answered Oct 05 '22 21:10

ta.speot.is


[ EDIT ]

Okay, after OP's edit, it seems I completely misunderstood the question. 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. Define print so that it tries to print the value ignoring any error that occurs while printing. And there you have it, a language that behaves as described in your question.

Code example in 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)

like image 29
missingfaktor Avatar answered Oct 05 '22 23:10

missingfaktor