/*
 * @topic T11380 Exception demo VII
 * @brief using instanceof and | operators
*/

package week7exceptions;

import java.io.IOException;
import java.util.Scanner;

public class AppMain {

    public static void main(String[] args) /*throws IOException*/ {
        ExDemo demo = new ExDemo();
        try {
            demo.saveFile();
        } catch ( IOException ex ) {
            System.out.println("IOException was caught");
        }

        //demo = new ExDemoSpecific();
        
        if ( demo instanceof ExDemo ) {
            System.out.println("demo is instance of ExDemo");
        } /*else*/ if ( demo instanceof ExDemoSpecific ) {
            System.out.println("demo is instance of ExDemoSpecific");
        }
        
        Scanner sc = new Scanner(System.in);
        int value;
        for (;;) {
            try {
                System.out.print("Enter first integer: ");
                value = sc.nextInt();
                break;
                //} catch ( java.util.InputMismatchException ex ) {
            } catch (java.util.InputMismatchException | java.lang.IllegalStateException ex) {
            } catch (java.lang.RuntimeException ex) {
                System.err.println("1st Bad input");
                //System.exit(0);
                //if ( ex.getCause() != null ) {
                //    System.out.println( "Cause: " + ex.getCause().toString() );
                //}
                //System.out.println( "Message: " + ex.getMessage() );
                System.err.println("Stack trace: [[[");
                ex.printStackTrace();
                System.err.println("]]] Stack trace");
                sc.nextLine(); // ignore any bad input
                continue;
            } finally {
                System.out.println("1st finally always executes!");
            }
        }//endless loop

        try {
            System.out.print("Enter second integer: ");
            value = sc.nextInt();
        } catch (java.util.InputMismatchException ex) {
            System.out.println("2nd Bad input");
            //System.exit(0);
        } finally {
            System.out.println("2nd finally always executes!");
        }
    }//main
}//class AppMain