This example demonstrates the usage of ParseException class and when does this exception occur with an example.
The java.text.ParseException object signals that an error has been reached unexpectedly while parsing.
ParseException Class Diagram
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)
Reference
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course