Java ProgramsExceptionsNumberFormatException

NumberFormatException in Java

beginner·  Exceptions  ·  Error Handling

Problem

A NumberFormatException is thrown when Integer.parseInt() (or similar parsing methods) receives a string that doesn't represent a valid number.

Parse a string that isn't a valid number into an int, and handle the resulting exception.

Input
Integer.parseInt("abc")
Output
Error: 'abc' is not a valid number

Java Program

Java
public class NumberFormatExceptionDemo { public static void main(String[] args) { String input = "abc"; try { int number = Integer.parseInt(input); // "abc" has no valid digit sequence System.out.println("Parsed: " + number); } catch (NumberFormatException e) { System.out.println("Error: 'abc' is not a valid number"); } } }

Output

Error: 'abc' is not a valid number

Core Logic

Wrapping the parse attempt in try-catch lets an invalid numeric string be reported cleanly instead of crashing the program.

How It Works
  1. 1String input = "abc"; holds text that doesn't represent any integer.
  2. 2Integer.parseInt(input) tries to convert it to an int, but there's no valid digit sequence to parse.
  3. 3This throws a NumberFormatException at the moment of parsing.
  4. 4The catch (NumberFormatException e) block catches it and prints a message instead of letting the program crash.
Parsing "abc" throws immediately, caught and printed as "Error: 'abc' is not a valid number".
💡

Key Point: parseInt() has no partial-success mode — a string is either entirely a valid integer or the whole call fails, even if only one character is invalid.

Key Concepts

try / catchInteger.parseInt()NumberFormatException

Approach 2: Java 8

Java
import java.util.Optional; public class NumberFormatExceptionOptional { static Optional<Integer> tryParse(String s) { try { return Optional.of(Integer.parseInt(s)); } catch (NumberFormatException e) { return Optional.empty(); // failure becomes an empty Optional instead of a thrown exception } } public static void main(String[] args) { String input = "abc"; String result = tryParse(input) .map(n -> "Parsed: " + n) .orElse("Error: 'abc' is not a valid number"); System.out.println(result); } }

Output

Error: 'abc' is not a valid number

Core Logic

Optional can represent 'a parsed value, or nothing' directly — attempt the parse inside a helper that turns failure into an empty Optional, then let map()/orElse() handle both outcomes.

How It Works
  1. 1A small helper tryParse(String s) wraps Integer.parseInt(s) in its own try-catch, returning Optional.of(result) on success or Optional.empty() on failure — this is what lets the failure be represented as a value instead of a thrown exception further down the pipeline.
  2. 2tryParse(input) returns an empty Optional for "abc", since the parse inside it fails.
  3. 3.map(n -> "Parsed: " + n) only runs if the Optional is present, formatting the successful result.
  4. 4.orElse("Error: 'abc' is not a valid number") supplies the fallback message if the Optional was empty.
For input = "abc", tryParse() returns Optional.empty(), so .map() is skipped and .orElse(...) supplies the error message.
💡

Key Point: The try-catch doesn't disappear here — it just moves inside a small reusable helper, so the calling code reads as a value pipeline instead of a control-flow block.

Key Concepts

Optionalmap()orElse()

Related Programs