Blog |

How to Fix the Unknown Format Conversion Exception in Java

How to Fix the Unknown Format Conversion Exception in Java
Table of Contents

The UnknownFormatConversionException is an unchecked exception in Java that occurs when an unknown conversion is given. Since the UnknownFormatConversionException is thrown at runtime, it does not need to be declared in the throws clause of a method or constructor.

 

What Causes UnknownFormatConversionException

The UnknownFormatConversionException is thrown when an unknown conversion occurs while formatting data. This can happen when using a format specifier that has an unknown parameter value. For example, using the value %i as a format specifier will throw an UnknownFormatConversionException, since this is an unknown parameter value.

 

UnknownFormatConversionException Example

Here is an example of an UnknownFormatConversionException thrown when using a format specifier that has an unknown parameter value:

public class UnknownFormatConversionExceptionExample {
    public static void main(String args[]) {
        String str = "Hello World";
        System.out.printf("%w", str);
    }
}

Since %w is an unknown value for a format specifier, using it throws an UnknownFormatConversionException:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'w'
    at java.base/java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2839)
    at java.base/java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2865)
    at java.base/java.util.Formatter.parse(Formatter.java:2713)
    at java.base/java.util.Formatter.format(Formatter.java:2655)
    at java.base/java.io.PrintStream.format(PrintStream.java:1209)
    at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
    at UnknownFormatConversionExceptionExample.main(UnknownFormatConversionExceptionExample.java:4)

 

How to Fix UnknownFormatConversionException

To avoid the UnknownFormatConversionException, it should be ensured that format specifiers used in code have a known value.

In the above example, the exception can be resolved by replacing %w with %s which is the correct format specifier for string values.

public class UnknownFormatConversionExceptionExample {
    public static void main(String args[]) {
        String str = "Hello World";
        System.out.printf("%s", str);
    }
}

Running the above code produces the correct output as expected:

Hello World

 

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape