Scala Program to Demonstrate the Throws Keyword

You are currently viewing Scala Program to Demonstrate the Throws Keyword

Scala program to demonstrate the throws keyword

// Scala program to demonstrate the
// "throws" keyword

object Sample {
  @throws(classOf[ArithmeticException])
  def divide(num1: Int, num2: Int): Int = {
    return (num2 / num1);
  }
  // Main method
  def main(args: Array[String]) {
    var num1: Int = 2;
    var num2: Int = 10;
    var res: Int = 0;

    try {
      res = divide(num1, num2);
      printf("Result: %d\n", res);
    } catch {
      case e: ArithmeticException => println(e);
    } finally {
      println("Finally block executed")
    }
  }
}

output

Result: 5
Finally block executed

Ranjith Kumar

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 Reply