Showing posts with label catch. Show all posts
Showing posts with label catch. Show all posts
Thursday, September 3, 2009
Exception handling
In Scala exceptions are not checked so effectively all exceptions are runtime exceptions. When you want to handle exceptions you use a
try {...} catch {...}
block like you would in Java except that the catch block uses matching to identify and handle the exceptions. This creates a very powerful but light-weight way to handle exceptions:- scala> def handle( f: => Unit ) = {
- | try { f } catch {
- | case _:AssertionError => println ("Whoops an assertion error")
- | case r:RuntimeException => println ("Runtime Exception: "+ r.getStackTraceString)
- | case e if (e.getMessage == null) => println ("Unknown exception with no message")
- | case e => println ("An unknown error has been caught" + e.getMessage)
- | }
- | }
- handle: (=> Unit)Unit
- scala> handle { throw new AssertionError("big bad error") }
- Whoops an assertion error
- scala> handle { throw new IllegalArgumentException("Sooooo illegal") }
- Runtime Exception: line9$object$$iw$$iw$$iw$$anonfun1ドル.apply(
:8) - line9$object$$iw$$iw$$iw$$anonfun1ドル.apply(
:8) - line7$object$$iw$$iw$$iw$.handle(
:7) - line9$object$$iw$$iw$$iw$.
(:8) - line9$object$$iw$$iw$$iw$.
() - RequestResult$line9$object$.
(:3) - RequestResult$line9$object$.
() - RequestResult$line9$object.result(
) - sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- java.lang.reflect.Method.invoke(Method.java:597)
- scala.tools.nsc.Interpreter$Request.loadAndRun(Interpreter.scala:889)
- scala.tools.nsc.Interpreter.interpret(Interpreter.scala:508)
- scala.tools.nsc.Interpreter.interpret(Interpreter.scala:494)
- scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scala:242)
- scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:230)
- scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:142)
- scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:298)
- scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:141)
- scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
- scala> handle{ throw new java.io.IOException("cant read something") }
- An unknown error has been caughtcant read something
Monday, August 17, 2009
Return values
As with most functional languages, most control structures ( if, for, try ) return values. The common java idiom:
can be replaced by
The benefit (other than less boiler plate code) is that name can now be a val instead of a var.
Another other point about returns: The return keyword is not required when returning a value from methods or control structures. The last value is always the return value. This is why you will get an error if the last line in a method or control structure is an assignment.
Examples:
- String name=null;
- if( xxx ) name="yyy";
- else name="zzz";
can be replaced by
- val name = if( xxx ) "yyy"; else "zzz";
The benefit (other than less boiler plate code) is that name can now be a val instead of a var.
Another other point about returns: The return keyword is not required when returning a value from methods or control structures. The last value is always the return value. This is why you will get an error if the last line in a method or control structure is an assignment.
Examples:
- scala> val name = if( 1==2 ) "Jesse" else "Mauricio"
- name: java.lang.String = Mauricio
- scala> println(name)
- Mauricio
- scala> val collection = for( i <- 1 to 100; if(i%20 == 3) ) yield i
- collection: Seq.Projection[Int] = RangeFM(3, 23, 43, 63, 83)
- scala> collection.foreach( i => print( i +" ") )
- 3 23 43 63 83
- scala> val someObj:AnyRef = "Hello"
- someObj: AnyRef = Hello
- scala> val choice = someObj match {
- | case _:java.io.File => "File"
- | case _:String => "String"
- | case _ => "Dunno"
- | }
- choice: java.lang.String = String
- scala> val result = try {
- | "two".toInt
- | }catch{
- | case e:NumberFormatException => -1
- | case _ => 0
- | }
- result: Int = -1
- scala> var i=0
- i: Int = 0
- // while and do-while do not have return values
- scala> while( i<4 ){
- | "22"
- | i += 2
- | }
- scala> println( if(i>0) "great" else "less" )
- great
- // code blocks return the last statement
- scala> val m = {
- | val x = 1
- | x + 2
- | }
- m: Int = 3
Subscribe to:
Comments (Atom)