Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,10 @@
{{Control Structures}}
This task is to give an example of an exception handling routine and to "throw" a new exception.
{{omit from|Euphoria}}
{{omit from|M4}}
{{omit from|Retro}}
{{omit from|Swift|[Swift does not support exception handling]}}
This task is to give an example of an exception handling routine
and to "throw" a new exception.
Cf. [[Exceptions Through Nested Calls]]

View file

@ -2,18 +2,21 @@ package main
import "fmt"
func foo() {
fmt.Println("let's foo...")
defer func() {
if err := recover(); err != nil {
fmt.Printf("Error: %v\n", err)
}
}()
panic("FAIL!")
fmt.Println("there's no point in going on.")
func foo() int {
fmt.Println("let's foo...")
defer func() {
if e := recover(); e != nil {
fmt.Println("Recovered from", e)
}
}()
var a []int
a[12] = 0
fmt.Println("there's no point in going on.")
panic("never reached")
panic(fmt.Scan) // Can use any value, here a function!
}
func main() {
foo()
fmt.Println("glad that's over.")
foo()
fmt.Println("glad that's over.")
}

View file

@ -0,0 +1,23 @@
:- object(exceptions).
:- public(double/2).
double(X, Y) :-
catch(double_it(X,Y), Error, handler(Error, Y)).
handler(error(not_a_number(X), logtalk(This::double(X,Y), Sender)), Y) :-
% try to fix the error and resume computation;
% if not possible, rethrow the exception
( catch(number_codes(Nx, X), _, fail) ->
double_it(Nx, Y)
; throw(error(not_a_number(X), logtalk(This::double(X,Y), Sender)))
).
double_it(X, Y) :-
( number(X) ->
Y is 2*X
; this(This),
sender(Sender),
throw(error(not_a_number(X), logtalk(This::double(X,Y), Sender)))
).
:- end_object.

View file

@ -0,0 +1,62 @@
//Defining exceptions
class AccountBlockException extends Exception
class InsufficientFundsException(val amount: Double) extends Exception
class CheckingAccount(number: Int, var blocked: Boolean = false, var balance: Double = 0.0) {
def deposit(amount: Double) { // Throwing an exception 1
if (blocked) throw new AccountBlockException
balance += amount
}
def withdraw(amount: Double) { // Throwing an exception 2
if (blocked) throw new AccountBlockException
if (amount <= balance) balance -= amount
else throw new InsufficientFundsException(amount - balance)
}
}
object CheckingAccount extends App {
class ExampleException1 extends Exception
val c = new CheckingAccount(101)
println("Depositing $500...")
try {
c.deposit(500.00)
println("\nWithdrawing $100...")
c.withdraw(100.00)
println("\nWithdrawing $600...")
c.withdraw(600.00)
} catch { // Exception handler
case ac: InsufficientFundsException => println(s"Sorry, but you are short ${'$'} ${ac.amount}")
case ac: AccountBlockException => println("Account blocked.")
///////////////////////////// An example of multiple exception handler ////////////////////////
case e@(_: ExampleException1 |
_: InterruptedException) => println(s"Out of memory or something else.")
case e: Exception => e.printStackTrace()
case _: Throwable => // Exception cached without any action
} finally println("Have a nice day")
}
object CheckingBlockingAccount extends App {
val c = new CheckingAccount(102, true)
println("Depositing $500...")
try {
c.deposit(500.00)
println("\nWithdrawing $100...")
c.withdraw(100.00)
println("\nWithdrawing $600...")
c.withdraw(600.00)
} catch { // Exception handler
case ac: InsufficientFundsException => println(s"Sorry, but you are short ${'$'} ${ac.amount}")
case ac: AccountBlockException => println("Account blocked.")
case e: Exception => e.printStackTrace()
case _: Throwable =>
} finally println("Have a nice day")
}
object NotImplementedErrorTest extends App {
??? // Throws scala.NotImplementedError: an implementation is missing
}