views:

214

answers:

4

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.

+6  A: 

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.

taspeotis
+1 for cheekiness with VB, but avoids breaking code flow with try/catch.
gbn
+2  A: 

[ 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)

missingfaktor
+1  A: 

In Mathematica you don't need an error catching command

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:

 Print[1/n]  

gives

 0
belisarius
A: 

Any language that uses IEEE 754 floating point arithmetic. Divided by zero is Infinity.

For example in Javascript:

> 1/0
Infinity
Chetan Sastry