Handling NumberFormatException Example
This program demonstrates handling NumberFormatException.
Problem Statement
Write a Java program that handles invalid number conversion using try-catch.
Source Code
| 1 | public class NumberFormatExceptionExample { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq NumberFormatException Example"); |
| 4 | try { |
| 5 | String s="abc"; |
| 6 | int num=Integer.parseInt(s); |
| 7 | System.out.println("Eduinq Number: "+num); |
| 8 | } catch(NumberFormatException e) { |
| 9 | System.out.println("Eduinq Exception: Invalid Number Format"); |
| 10 | } |
| 11 | } |
| 12 | } |
Program Output
Eduinq NumberFormatException Example Eduinq Exception: Invalid Number Format
Explanation
NumberFormatException occurs when parsing non-numeric strings to numbers. - Try-catch prevents program crash and provides meaningful error message. - This design improves input validation and error handling. - In enterprise systems, number format checks are critical for data integrity. Conclusion: Shows handling of NumberFormatException.