setjmp
/longjmp
(a.k.a. non-local goto)
try
-catch
blocks, finally
panic
and resume
for special cases
// Signature:
func Open(name string) (file *File, err error)
// Usage:
f, err := os.Open("filename.ext")
if err != nil {
log.Fatal(err)
}
// Do something with the open *File f
import com.atomicscala.AtomicTest._
def f(i:Int) =
if(i == 0)
Left("Divide by zero")
else
Right(24/i)
def test(n:Int) =
f(n) match {
case Left(why) => s"Failed: $why"
case Right(result) => result
}
test(4) is 6
test(5) is 4
test(6) is 4
test(0) is "Failed: Divide by zero"
test(24) is 1
test(25) is 0
import com.atomicscala.AtomicTest._
import util.{Try, Success}
import com.atomicscala.reporterr.Fail
def f(i:Int) =
if(i == 0)
Fail("Divide by zero")
else
Success(24/i)
def test(n:Int) = f(n).recover{
case e => s"Failed: $e"
}.get
test(4) is 6
test(5) is 4
test(6) is 4
test(0) is "Failed: Divide by zero"
test(24) is 1
test(25) is 0
def f(n):
if n > 10:
return n * 10, False
else:
return None, True
def test(n):
r, fail = f(n)
if fail:
print n, "failed"
else:
print r
test(9)
test(11)
class Result(object):
def __init__(self, result, fail=False):
self.result = result
self.fail = fail
class Fail(Result):
def __init__(self, reason=True):
Result.__init__(self, None, reason)
def f(n):
if n > 10:
return Result(n * 10)
else:
return Fail("%d is too small" % n)
def test(n):
r = f(n)
if r.fail is not False:
print r.fail
else:
print r.result
test(9)
test(11)
class Result(object):
def __init__(self, result, fail=False):
self.__result = result
self.fail = fail
@property
def result(self): # Getter
if self.fail is False:
return self.__result
else:
raise Exception(
"Accessed Result.result during failure",
self.fail)
class Fail(Result):
def __init__(self, reason=True):
Result.__init__(self, None, reason)
def f(n):
if n > 10:
return Result(n * 10)
else:
return Fail("%d is too small" % n)
def test(n):
r = f(n)
if r.fail is not False:
print r.fail
else:
print r.result
test(9)
test(11)
f(9).result
# result.py
class ResultError(Exception):
def __init__(self, reason):
Exception.__init__(self,
"Accessed Result.result during failure",
reason)
class Result(object):
def __init__(self, result, fail=False):
self.__result = result
self.fail = fail
@property # Getter
def result(self):
if self.fail is False:
return self.__result
else:
raise ResultError(self.fail)
class Fail(Result):
def __init__(self, reason=True):
Result.__init__(self, None, reason)
from result import *
def f(n):
if n > 10:
return Result(n * 10)
else:
return Fail("%d is too small" % n)
def test(n):
r = f(n)
if r.fail is not False:
print r.fail
else:
print r.result
test(9)
test(11)
f(9).result
map(print, f(n))