How Can We Use Scala Throw Keyword – Scala Exception   Recently updated !


1. Objective

In this tutorial, we will talk about the Scala Throw Keyword. So, in this Scala Throw keyword Tutorial, we are going to see how can we Throw Custom Exception in Scala Programming Language. Moreover, we will discuss Catching Exceptions in Scala.

Let’s begin Sacla Throw Keyword.

How Can We Use Scala Throw Keyword - Scala Exception

How Can We Use Scala Throw Keyword – Scala Exception

2. How Can We Throw Custom Exceptions in Scala?

Scala lets you create your own custom exceptions using the Scala Throw Keyword. With this, you can explicitly throw an exception in your code.

Let’s Get a Clear Information About Scala Exceptions and Exception Handling

Let’s take a simple example.

scala> def person(age:Int){
| if(age!=15)
| throw new Exception("Wait a little")
| else println("Enjoy your quinceanera")
| }
person: (age: Int)Unit
scala> person(15)

Enjoy your quinceanera

scala> person(14)

java.lang.Exception: Wait a little

at .person(<console>:13)
... 28 elided

3. Scala Throw Keyword

Now, when we know a method can throw an exception, we may want to tell Scala. We use the Scala throws keyword for this, but we can also place the @throws annotation right before a method.

@throws(classOf[Exception])
override def play{
//exception-throwing code
}

But a method may throw more than one code. To mention all of these, we use multiple annotations in Scala.

@throws(classOf[IOException])
@throws(classOf[LineUnavailableException])
@throws(classOf[UnsupportedAudioFileException])
def playSoundFileWithJavaAudio{
//exception-throwing code
}

@throws is also a way to provide the method signature for ‘throws’ to those who work with Java Programming Language. Java would have the following code for this:

public void play() throws FooException{
//code
}

Do you Know 2 Popular Types of Constructors in Scala

4. More On Throw Keyword in Scala

Talking about checked exceptions, Scala doesn’t need methods to declare that they can throw exceptions. In Scala, all exceptions are RuntimeExceptions, and it is the developer who must decide when to handle them. It doesn’t force us to handle these exceptions.

scala> def demo{
| throw new Exception
| }

demo: Unit

We don’t even need to call these methods to catch the exceptions.

scala> demo
java.lang.Exception
at .demo(<console>:12)
... 28 elided

But we must test for them if we want our code to work fine:

scala> object Demo extends App{
| def demo{
| throw new Exception}
| println("After demo")
| demo
| //Code never executed
| println("After demo")
| }

defined object Demo

Read about Scala Regular Expressions – Replacing Matches

5. Catching Exceptions in Scala

In the following example, we throw an exception and then have a wildcard pattern case catch it. This pattern catches any exception of the kind throwable.

scala> try{
| throw new Exception("Failed")
| } catch{
| //catching all Throwable exceptions
| case _:Throwable=>println("An exception occurred")
| }

An exception occurred

6. Example- Exceptions With Pattern Matching

Let’s take another example. Here, we combine pattern-matching with exception handling. This is a bit like the previous example.

scala> import java.net.URL
import java.net.URL
scala> import java.net.MalformedURLException
import java.net.MalformedURLException
scala> import java.io.IOException
import java.io.IOException
scala> try{
| val url=new URL("http://data-flair.training")
| }catch{
| case e:MalformedURLException=>println("Bad URL "+e)
| case e:IOException=>println("An IO issue "+e)
| case _:Throwable=>println("Something else")
| }finally{
| //cleanup
| }

Let’s Explore Scala Singleton & Scala Companion Object

So, this was all about Scala Throw Keyword. Hope you like our explanation.

7. Conclusion

Hence, in this Scala Throw tutorial, we have seen how we can throw custom exceptions in Scala. Drop your queries in the comments.

Related Topic-  How to Implement Scala Abstract Class with Examples

For reference

Leave a comment

Your email address will not be published. Required fields are marked *