Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -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]]
|
||||
|
|
|
|||
|
|
@ -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.")
|
||||
}
|
||||
|
|
|
|||
23
Task/Exceptions/Logtalk/exceptions.logtalk
Normal file
23
Task/Exceptions/Logtalk/exceptions.logtalk
Normal 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.
|
||||
62
Task/Exceptions/Scala/exceptions.scala
Normal file
62
Task/Exceptions/Scala/exceptions.scala
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue