Created
January 16, 2022 20:28
-
-
Save deanwampler/e67c664f9a1ef1fcd45b8f3bc50de616 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 | |
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