Formatter ioException() method in Java with Examples

The ioException() method is a built-in method of the java.util.Formatter which returns the IOException last thrown by this formatter’s Appendable. If the destination’s append() method never throws IOException, then this method will always return null.

Syntax:

public IOException ioException()

Parameters: The function accepts no parameter.



Return Value: The function returns the IOException which was last thrown by the Formatter’s appendable.

Below is the implementation of the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
  
import java.util.Formatter;
import java.util.Locale;
  
public class Main {
  
    public static void main(String[] args)
    {
  
        // Get the string Buffer
        StringBuffer buffer
            = new StringBuffer();
  
        // Object creation
        Formatter frmt
            = new Formatter(buffer,
                            Locale.CANADA);
  
        // Format a new string
        String name = "My name is Gopal Dave";
        frmt.format("What is your name? \n%s !",
                    name);
  
        // Print the Formatted string
        System.out.println(frmt);
  
        // flushes the formatter
        frmt.flush();
        System.out.println("Flushed");
    }
}

chevron_right


Output:

What is your name? 
My name is Gopal Dave !
Flushed

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
  
import java.util.Formatter;
import java.util.Locale;
  
public class Main {
  
    public static void main(String[] args)
    {
  
        // Get the string Buffer
        StringBuffer buffer
            = new StringBuffer();
  
        // Object creation
        Formatter frmt
            = new Formatter(buffer,
                            Locale.CANADA);
  
        // Format a new string
        String name = "My name is Gopal Dave";
        frmt.format("What is your name? \n%s !",
                    name);
  
        System.out.println("The last exception thrown: "
                           + frmt.ioException());
  
        // Closes the format
        frmt.close();
    }
}

chevron_right


Output:

The last exception thrown: null
Output:

The last exception thrown: null

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#flush()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.