Skip to content

Instantly share code, notes, and snippets.

@pjambet

pjambet/FakeService.java

Last active Jan 4, 2020
Embed
What would you like to do?
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