Scala

Scala Try Catch

Exceptions in any programming language are defined as the unexpected behavior resulting from executing an irrelevant or logically incorrect piece of code. We use the “Try Catch” expression in Scala to handle these exceptions gracefully. Within the “try” block, we write code that is likely to throw an error, whereas we cater to those exceptions within the “catch” block. This concept is pretty much similar to the one used in Java; however, the only difference is that the “Try Catch” expression in Scala works on the concept of pattern matching, and you will learn more about this by going through this article.

Examples of using the Try Catch Expression of Scala:

For understanding how the Try Catch expression can be used in Scala, you will have to go through the following instances that we have implemented on a Ubuntu 20.04 system.

Example # 1: Catching an Arithmetic Exception with the Try Catch Expression in Scala:

Arithmetic exceptions are most likely to occur while performing mathematical calculations in Scala. The occurrence of these exceptions implies that some rules of mathematics have been violated. To learn the process of catching an arithmetic exception in Scala, you will have to take a look at the following code:

In this example, we have first created a “TryCatch” class in Scala. Then, within the “main()” function of our program, we have a “try” block inside which we have an expression that divides a number by “0”. We all know that in mathematics, any number divided by zero leads to an error since it is equal to infinity. Therefore, this expression in Scala is also likely to throw an exception that we will be catching in the “catch” block of our code. Within the “catch” block, we have a case with the title “exception.” We have named this exception as an “ArithmeticException.” We have printed a message on the terminal within this case statement to inform the user that an arithmetic exception has occurred.

To see how this Scala script works, we first compiled it with the command shown below:

$ scalac TryCatch.Scala

Then, we had to execute it with the following command:

$ scala TryCatch

The output of this Scala script is shown in the image below, which confirms that an arithmetic exception has occurred in our Scala script since we attempted to divide a number by “0”.

Example # 2: Using the Try Catch Expression in Scala with Multiple Case Statements:

In the example discussed above, we only made use of a single “case” statement since we were sure of the exception that could be thrown at us. However, when you are unsure of the exception that will be thrown due to running a particular piece of code, you need multiple different expressions for catching these exceptions. However, in Scala, instead of having multiple “catch” expressions, you can have multiple cases within the same “catch” block. In this way, the “catch” expression will work on the principle of pattern matching, and the most suitable exception out of all the cases according to the situation will be presented to the user. This is demonstrated by the following Scala script:

We have included some libraries prior to writing our actual code in this example. These libraries contain the implementation of the exceptions that we will be using in this script. After including these libraries, we declared a “TryCatch” class within which there is a “main()” function. Inside this function, we have a “try” block in which we have created a value named “reading” and tried to read a file with the name “abc.txt”. Note that this file did not exist on our system, which means that an error will be shown as a result of running this code. Then, within our “catch” block, we have two different “case” statements. The first one will be executed if a “FileNotFound” error is shown, which holds true in this case. However, if it is an “IO” exception, then the second case of the “catch” block will be executed.

Once we compiled and executed this script, a “FileNotFound” exception appeared on the terminal because we tried to read a file that did not exist on our system.

Example # 3: Using the Try Catch Expression with the Finally Expression in Scala:

At times, you write a lengthy script. You are not sure about a certain part of that script and whether it will be executed successfully or not; however, you still want some other part of that script to be executed regardless of the occurrence of any exception. In that case, you must write that part of your script within the “finally” expression. This can be seen in the following script:

You can see in the images shown above that this script is the same as we discussed in our second example; however, the only part that we have added to this script is the “finally” expression. Within this expression, we have simply printed a random message; however, you can write any piece of code within this expression that you want to be executed in any way.

The image shown below displays the output of this Scala script. You can see that although an exception occurred due to running this script, the statement was written inside the “finally” block still got executed.

Conclusion:

This guide was meant for explaining the concept of “Try Catch” in Scala in Ubuntu 20.04. Firstly, we taught you the purpose of this expression in Scala and commented on its resemblance with Java. Then, to clearly explain its usage, we explained to you three different examples that implemented this expression in Scala. We also highlighted how you could cater to multiple exceptions simply by using multiple “cases” within a single “catch” block. After learning these basic concepts, you will be able to efficiently write programs in the Scala programming language.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.