Scala Exceptions | How Scala Methods Throw Exceptions?

Scala exceptions | How Scala methods throw exceptions?

import java.io.IOException 
object MyClass 
{ 
	def main(args:Array[String]) 
	{   
	    var i= 3 
	    var j = 3 
		try
		{ 
			var N = 5/i - j
			println("The division is successful the value is :" + 5/(i-j))
		} 
		catch
		{ 
			case  e : ArithmeticException =>
			{ 
				println("Arithmetic exception occurred.") 
			}
		} 
       
	} 
} 
import java.io.IOException 
object MyClass 
{ 
	def main(args:Array[String]) 
	{   
	    var i= 3 
	    var j = 3 
		try{ 
			var N = 5/i - j
			println("the division is successful the value is :" + 5/(i-j))
		} 
		catch{ 
			case  e : ArithmeticException =>{ 
				println("Arithmetic exception occurred.") 
			}
		} 
		finally {
		    println("The finally code that runs in all cases...")
		}
       
	} 
} 
The above code has 2 two outputs based on the value of I and J

case 1 : i= j
Arithmetic exception occurred

case 2 : i !=j
The division is successful the value is -5 

Note: **the value is evaluated based on the value entered in the code 
Arithmetic exception occurred.
The finally code that runs in all cases...
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment