Created
January 21, 2016 08:19
-
-
Save momota10/9554ba756c467e60e05c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ##例外処理 | |
| scala> val n = try{"99".toInt} catch {case e:Exception => -99} | |
| n: Int = 99 | |
| scala> val n = try{"999".toInt} catch {case e:Exception => -99} | |
| n: Int = 999 | |
| scala> val n = try{ "momo".toInt } catch { case e:Exception => -99 } | |
| n: Int = -99 | |
| ##例外処理2 | |
| scala> val n = try{ "momo".toInt } catch { | |
| | case e:Exception => -1 } finally { println("finally"); } | |
| finally | |
| n: Int = -1 | |
| ##例外処理1 | |
| scala> def toIntOption(s: String):Option[Int] = try{Some(s.toInt)} catch{case _ => None} | |
| toIntOption: (s: String)Option[Int] | |
| scala> val n = toIntOption( "foo" ) getOrElse( -1 ) | |
| n: Int = -1 | |
| scala> val n = toIntOption( "100" ) getOrElse( -1 ) | |
| n: Int = 100 | |
| scala> val n = toIntOption( "-100" ) getOrElse( -1 ) | |
| n: Int = -100 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment