Express Java try-catch-finally in Scala's scala.util.Try

Java's try-catch-finally does not catch a fatal error, Scala's try-catch-finally also catches fatal errors. Well-known fatal exceptions include ʻOutOfMemoryError`, and such fatal exceptions should not be caught in the program.

scala> try {
     |   throw new StackOverflowError
     | } catch {
     |   case e: Throwable => println("error")
     | }
error

Therefore, in Scala, it is common (?) To catch exceptions using scala.util.Try instead of try-catch-finally. scala.util.Try, like Java's try-catch-finally, only catches non-fatal errors (NonFatalError).

scala> Try(throw new StackOverflowError) recover {
     |   case e: Throwable => println("error")
     | }
java.lang.StackOverflowError
  at .$anonfun$res13$1(<console>:13)
  at scala.util.Try$.apply(Try.scala:209)
  ... 38 elided

But scala.util.Try becomes Failure when an exception is thrown, and there is no mechanism like finally to do something at the end. I want to finally in Scala. I think there is such a time.

In such a case, there is a way to use scala.util.control.Exception.ultimately. Finally can be achieved by passing the last process to be executed regardless of whether an exception occurs.

scala> Try {
     |   ultimately {
     |     println("finally !")
     |   } {
     |     println("do something")
     |     throw new Exception
     |     println("no execute")
     |   }
     | }
do something
finally !
res: scala.util.Try[Unit] = Failure(java.lang.Exception)

In this way, you can ignore fatal errors and only catch non-fatal errors. Of course, if you don't need to use finally, you don't need ʻultimately` either.

References

There are only two Japanese articles about scala.util.control.Exception.

Postscript

As you commented, Scala's try-catch-finally also Pattern matching with NonFatal in catch can only catch non-fatal ones.

NonFatal Exception --scala_text

Recommended Posts

Express Java try-catch-finally in Scala's scala.util.Try
Express failure in Java
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Write flyway callbacks in Java
Topic Analysis (LDA) in Java
Importing Excel data in Java 2
NEologd preprocessing in Java neologdn-java
Change java encoding in windows
Java Stream API in 5 minutes
Cannot find javax.annotation.Generated in Java 11