Unchecked exception thrown when an unknown conversion is given.
Unless otherwise specified, passing a null argument to any method or constructor in this class will cause a NullPointerException to be thrown.
NullPointerException in String contains method.
UnknownFormatConversionException is child class of IllegalFormatException class.
public UnknownFormatConversionException(String s)
Constructs an instance of this class with the unknown conversion. s indicates the unknown conversion type or value.
1) public String getConversion(): Returns the unknown conversion.
2) public String getMessage(): the detail message string of this Throwable instance (which may be null). This method is overridden method from Throwable class.
The following code throws this exception, and I'm not sure why. There is clearly a problem with outputting String.format to the formatedStr variable, but I don't know what's wrong with it.
package examples.java.w3schools.exceptions; public class UnknownFormatConversionException { public static void main(String[] args) { String name = "Jhon"; int age = 20; String formatedStr = String.format("Name : %s, Age : %i", name, age); System.out.println("formatedStr : " + formatedStr); } }
Output:
Below is the exception full stack trace.Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'i' at java.base/java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2839) at java.base/java.util.Formatter$FormatSpecifier.(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.util.Formatter.format(Formatter.java:2609) at java.base/java.lang.String.format(String.java:2897) at w3schools/examples.java.w3schools.exceptions.UnknownFormatConversionException.main(UnknownFormatConversionException.java:9)
We will understand where the problem is. Just observe the reason for the exception that is "Conversion = 'i'". It is indicating that some problem with conversion type i.
In above code, used "%i" for integer which is causing for the error.
String formatedStr = String.format("Name : %s, Age : %d", name, age);
formatedStr : Name : Jhon, Age : 20
String welcomeNote = String.format("Hello, Mr %# : Welcome to JavaW3schools blog : ", "Billy"); System.out.println("Welcome Note : " + welcomeNote);
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '#' at java.base/java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2839) at java.base/java.util.Formatter$FormatSpecifier.(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.util.Formatter.format(Formatter.java:2609) at java.base/java.lang.String.format(String.java:2897) at w3schools/examples.java.w3schools.exceptions.UnknownFormatConversionException.main(UnknownFormatConversionException.java:13)
This method returns the what conversion value causes for the problem. Refer the below example program.
public class UnknownFormatConversionExceptionMethods { public static void main(String[] args) { try { String welcomeNote = String.format("Hello, Mr %# : Welcome to JavaW3schools blog : ", "Billy"); System.out.println("Welcome Note : " + welcomeNote); } catch (java.util.UnknownFormatConversionException ex) { System.out.println("getConversion method value: " + ex.getConversion()); } } }
getConversion method value: #
package examples.java.w3schools.exceptions; public class UnknownFormatConversionExceptionMethods { public static void main(String[] args) { try { String welcomeNote = String.format("Hello, Mr %# : Welcome to JavaW3schools blog : ", "Billy"); System.out.println("Welcome Note : " + welcomeNote); } catch (java.util.UnknownFormatConversionException ex) { System.out.println("getMessage method value: " + ex.getMessage()); } } }
getMessage method value: Conversion = '#'
No comments:
Post a Comment
Please do not add any spam links in the comments section.