Created
January 16, 2022 20:20
-
-
Save deanwampler/0526edac70fd0d7bb066cf893655862d 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
package progscala3.rounding.saferexceptions | |
import java.io.{IOException, File} | |
import scala.annotation.experimental | |
// Enable safer exceptions | |
import language.experimental.saferExceptions | |
@experimental | |
object SaferExceptions: | |
def main(fileNames: Array[String]): Unit = | |
fileNames.foreach { fileName => | |
try | |
val file = openExistingFile(fileName) | |
val path = file.getCanonicalPath() | |
val size = file.length() | |
println(s"file $fileName ($path) has $size bytes.") | |
catch | |
case ioe: IOException => println(s"file $fileName: IOException caught: ${ioe.getMessage}") | |
} | |
def openExistingFile(fileName: String): File throws IOException = | |
val file = new File(fileName) | |
if file.exists() == false then throw new IOException(s"$fileName doesn't exist!") | |
file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment