ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Scala program to demonstrate the NumberFormatException

Here, we are going to demonstrate the NumberFormatException in Scala programming language.
Submitted by Nidhi, on June 08, 2021

Problem Solution:

Here, we will demonstrate NumberFormatException using the try and catch blocks.

Program/Source Code:

The source code to demonstrate the NumberFormatException is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to demonstrate the
// NumberFormatException

object Sample {
  // Main method
  def main(args: Array[String]) {
    var num: Int = 0;
    try {
      num = "Hello".toInt;
    } catch {
      case e: NumberFormatException => println(e);
    }
  }
}

Output:

java.lang.NumberFormatException: For input string: "Hello"

Explanation:

In the above program, we used an object-oriented approach to create the program.

And, we created a singleton object Sample and defined the main() function. The main() function is the entry point for the program.

In the main() function, we created an integer variable num initialized with 0. Then we tried to convert the "Hello" string into a number in the try block. That's why NumberFormatException gets generated that will be caught in the catch block and print an appropriate message on the console screen.

Scala Exception Handling Programs »

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.