logging

Log an exception

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: May 21st, 2013
0 153 1 minute read

With this example we are going to demonstrate how to log an exception. In order to do so, we will use aDateFormat and parse a String pattern to create a newDate. In short, to log theParseException that occurs you should:

  • Create a newSimpleDateFormat with a specific String pattern.
  • Invoke thesetLenient(boolean lenient) API method of the DateFormat, setting the lenient to false. Thus, the inputs of the DateFormat parser must match this object’s format, or else aParseException will be thrown.
  • Invoke thelog(Level level, String msg, Throwable thrown) API method to log a message, with the associatedThrowable information.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;import java.util.logging.Logger;import java.util.logging.Level;import java.util.Date;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.text.ParseException; public class LogException {    private static Logger logger = Logger.getLogger(LogException.class.getName());     public static void main(String[] args) {  DateFormat df = new SimpleDateFormat("dd/MM/yyyy");  df.setLenient(false);   try {// Set wrong dateDate date = df.parse("11/08/1984"); System.out.println("Date = " + date);  } catch (ParseException e) {  // Create log message if (logger.isLoggable(Level.SEVERE)) {    logger.log(Level.SEVERE, "Error parsing date", e);}  }    }}

Output:

Αυγ 12, 2012 1:30:09 ΜΜ com.javacodegeeks.snippets.core.LogException mainSEVERE: Error parsing datejava.text.ParseException: Unparseable date: "11/08/1984"at java.text.DateFormat.parse(Unknown Source)at com.javacodegeeks.snippets.core.LogException.main(LogException.java:22)

 
This was an example of how to log an exception in Java.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: May 21st, 2013
0 153 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Bipartite Graph

Conditional logging

November 11th, 2012
Bipartite Graph

Log method call

November 11th, 2012
Bipartite Graph

Set logger log level

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.