Skip to content

Java ParseException Example

Ramesh Fadatare edited this page Jul 12, 2019 · 1 revision

This example demonstrates the usage of ParseException class with an example.

The java.text.ParseException object signals that an error has been reached unexpectedly while parsing.

Java ParseException Example

In this example, we use DateFormat.parse(String source) method which throws ParseException object.

This parse() method throws ParseException - if the beginning of the specified string cannot be parsed.

Here is a complete code to throw ParseException exception:

package com.javaguides.corejava;

import java.text.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class ParseExceptionExample {

    public static void main(String[] args) {

        DateFormat format = new SimpleDateFormat("MM, dd, yyyy");

        try {
            format.parse("01, , 2010");
        } catch (ParseException e) {
            System.err.println("ParseException caught!");
            e.printStackTrace();
        }
    }
}

Output:

ParseException caught!
java.text.ParseException: Unparseable date: "01, , 2010"
	at java.text.DateFormat.parse(DateFormat.java:366)
	at com.javaguides.corejava.ParseExceptionExample.main(ParseExceptionExample.java:15)
Clone this wiki locally