Code example for Formatter

Methods: close, out

0
            throw new RuntimeException("NC.close should not be called");
        } 
    } 
 
    private static void test(Formatter f) {
        if (f.out() instanceof C) {
            // C.close() called since C implements Closeable 
            try { 
                f.close();
                throw new RuntimeException("C.close not called");
            } catch (ExpectedException x) {
                System.out.println("  C.close called");
            } 
        } else { 
            // NC.close() not called since NC does not implement Closeable 
            f.close();
        } 
 
        // Formatter is a Closeable 
        if (!(f instanceof Closeable))
            throw new RuntimeException("Formatter is not a Closeable");
 
        // multiple close() does not throw a FormatterClosedException 
        f.close();
        try { 
            f.close();
            System.out.println("  FormatterClosedException not thrown");
        } catch (FormatterClosedException x) {
            throw new RuntimeException("FormatterClosedException thrown");
        } 
    }