Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Created January 16, 2022 20:28
Show Gist options
  • Save deanwampler/e67c664f9a1ef1fcd45b8f3bc50de616 to your computer and use it in GitHub Desktop.
Save deanwampler/e67c664f9a1ef1fcd45b8f3bc50de616 to your computer and use it in GitHub Desktop.
package progscala3.rounding.saferexceptions
import java.io.{IOException, File}
import scala.annotation.experimental
import language.experimental.saferExceptions
@experimental
object SaferExceptionsNested:
def main(fileNames: Array[String]): Unit =
fileNames.foreach { fileName =>
try
val file = wrapper(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 wrapper(fileName: String): File throws IOException = openExistingFile(fileName)
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