Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Last active January 16, 2022 20:57
Show Gist options
  • Save deanwampler/20eca80661852e873dee344ff319b2f2 to your computer and use it in GitHub Desktop.
Save deanwampler/20eca80661852e873dee344ff319b2f2 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
/**
* Currently as written, this file doesn't compile using Scala 3.1.
* Also, the object needs the `@experimental` annotation in order to use this feature.
*/
@experimental // Must annotate all the types or methods that use the feature...
object SaferExceptions:
def main(fileNames: Array[String]): Unit =
fileNames.foreach { fileName =>
val file = openExistingFile(fileName)
val path = file.getCanonicalPath()
val size = file.length()
println(s"file $fileName ($path) has $size bytes.")
}
def openExistingFile(fileName: String): File throws IOException = // <1>
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