O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exceptions in Scala

Scala supports the Java semantics for exception handling, but the syntax for try-catch is quite different. Also, Scala does not distinguish between checked and unchecked exceptions—it treats all exceptions as if they’re unchecked.

In Scala you throw exceptions just like in Java; for example:

 
throw​ ​new​ IllegalArgumentException

Remember, you can leave out the empty parentheses after the class name when creating an instance and semicolons are optional.

Also, you can use a try block just like in Java. Scala, however, doesn’t force you to catch exceptions that you don’t care about—not even checked exceptions. This prevents you from adding unnecessary catch blocks—you simply let the exceptions you don’t care to catch propagate ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required