Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Created January 16, 2022 20:20
Show Gist options
  • Save deanwampler/0526edac70fd0d7bb066cf893655862d to your computer and use it in GitHub Desktop.
Save deanwampler/0526edac70fd0d7bb066cf893655862d to your computer and use it in GitHub Desktop.
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