package exceptions;

public class UninitializedException extends Exception {

    public UninitializedException(String message) {

        super(message);

    }

}

 

 

 

 

 

package exceptions;

public class ReinitializationException extends Exception {

    public ReinitializationException(String message) {

        super(message);

    }

}

 

 

 

 

 

package exceptions;

public class CheckedExample {

     

    private boolean initialized = false;

     

    public void initialize() throws ReinitializationException {

        if (initialized) {

            throw new ReinitializationException

                ("Object is already initialized");

        }

        initialized = true;

        System.out.println("I am initialized");

    }

     

    public void execute() throws UninitializedException {

        if (!initialized) {

            throw new UninitializedException

                 ("Object is already initialized");

        }

        System.out.println("I am executing");

    }

}

 

 

 

 

 

package exceptions;

public class Main {

    public static void main(String[] args) {

        CheckedExample ce = new CheckedExample();

        try {

            ce.initialize();

        } catch (ReinitializationException e) {

            System.out.println("ReinitializationException occurred");

        }

    }

}

 

 

 

 

 

package exceptions;

 

public class Main {

    public static void main(String[] args) throws ReinitializationException {

        CheckedExample ce = new CheckedExample();

        ce.initialize();

    }

}

 

 

 

 

 

package exceptions;

public class Main {

    public static void main(String[] args) {

        CheckedExample ce = new CheckedExample();

        try {

            ce.initialize();

        } catch (ReinitializationException e) {

            System.out.println("This will not be called.");

        }

        try {

            ce.initialize();

        } catch (ReinitializationException e) {

            System.out.println("This will be called.");

        }

    }

}

 

 

 

 

 

package exceptions;

public class Main {

    public static void main(String[] args) {

        CheckedExample ce = new CheckedExample();

        try {

            ce.execute();

        } catch (UninitializedException e) {

            try {

                ce.initialize();

                ce.execute();

            } catch (ReinitializationException e1) {}

              catch (UninitializedException e1) {}

        }

    }            

}

 

 

 

 

 

package exceptions;

public class Runtime {

    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer("Hello");

        StringBuffer sb2 = null;

        appendWorld(sb);

        appendWorld(sb2);

    }

     

    private static void appendWorld(StringBuffer sb) {

        sb.append(" World");

    }

}