Skip to content

Instantly share code, notes, and snippets.

@pjambet
Last active January 4, 2020 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjambet/44f017cfa2e332edfafc880f112d6d71 to your computer and use it in GitHub Desktop.
Save pjambet/44f017cfa2e332edfafc880f112d6d71 to your computer and use it in GitHub Desktop.
Checked and Unchecked exceptions in Scala
public class FakeService {
public class CustomCheckedException extends Throwable {}
public class CustomUncheckedException extends RuntimeException {}
public void doSomethingWithCheckedExceptions() throws CustomCheckedException {
throw new CustomCheckedException();
}
public void doSomethingWithUncheckedExceptions() {
throw new CustomUncheckedException();
}
}
object Main {
// Note that main does not need to declare that it can throws the checked exception CustomCheckedException
def main(args: Array[String]): Unit = {
val fs = new FakeService()
fs.doSomethingWithUncheckedExceptions() // Compiles and throws a CustomUncheckedException
fs.doSomethingWithCheckedExceptions() // Compiles and throws a CustomCheckedException
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment