DSblizzard
DSblizzard

Reputation: 4149

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.

Upvotes: 5

Views: 534

Answers (7)

Poul Sørensen
Poul Sørensen

Reputation: 1

Bourne Shell:

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

Upvotes: 0

Charles Maria
Charles Maria

Reputation: 2195

Dependently typed programming languages such as Idris, Agda or Coq will not compile if you ever try to do an illegal action. Here is a safe example of your code in 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.

Upvotes: 0

mjn
mjn

Reputation: 36664

Many years ago, our COBOL teacher in school used to say that 'COBOL is the only programming language which allows division by zero' (without giving a runtime error).

Upvotes: 0

Dr. belisarius
Dr. belisarius

Reputation: 61046

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

Upvotes: 1

missingfaktor
missingfaktor

Reputation: 92076

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

Upvotes: 2

Chetan S
Chetan S

Reputation: 23813

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

For example in Javascript:

> 1/0
Infinity

Upvotes: 1

ta.speot.is
ta.speot.is

Reputation: 27214

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.

Upvotes: 6

Related Questions