Last active
January 4, 2020 23:02
-
-
Save pjambet/44f017cfa2e332edfafc880f112d6d71 to your computer and use it in GitHub Desktop.
Checked and Unchecked exceptions in Scala
This file contains 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
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(); | |
} | |
} |
This file contains 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
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