28.08.2016 Views

scala_tutorial

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Scala<br />

}<br />

}<br />

}<br />

}<br />

Save the above program in Demo.<strong>scala</strong>. The following commands are used to compile and<br />

execute this program.<br />

\><strong>scala</strong>c Demo.<strong>scala</strong><br />

\><strong>scala</strong> Demo<br />

Output<br />

Missing file exception<br />

The behavior of this try-catch expression is the same as in other languages with exceptions.<br />

The body is executed, and if it throws an exception, each catch clause is tried in turn.<br />

The finally Clause<br />

You can wrap an expression with a finally clause if you want to cause some code to execute<br />

no matter how the expression terminates. Try the following program.<br />

import java.io.FileReader<br />

import java.io.FileNotFoundException<br />

import java.io.IOException<br />

object Demo {<br />

def main(args: Array[String]) {<br />

try {<br />

val f = new FileReader("input.txt")<br />

} catch {<br />

case ex: FileNotFoundException => {<br />

println("Missing file exception")<br />

}<br />

case ex: IOException => {<br />

println("IO Exception")<br />

162

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!