Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following pseudo-code: For the each part below determine the comple

ID: 3665564 • Letter: C

Question

Consider the following pseudo-code:

For the each part below determine the complete output if the pseudo-code above was run with the values of the q variables as specified below. Assume the exception classes Ex1, Ex2, and Ex3 each extend RuntimeException. If an exception is passed out of main, show the output of the runtime environment as "Program terminated due to Exception ExN ", where N is the particular exception number.

What would be output if q1 is true and the other variables are false?

What would be output if q2 is true and the other variables are false?

What would be output if q3 is true and the other variables are false?

How would the code need to be modified if exception type Ex1 was a checked exception?

Explanation / Answer

//class for Exception 1

class Ex1 extends RuntimeException
{
    Ex1(String s){
        super(s);
    }
}

//class for Exception2
class Ex2 extends RuntimeException
{
    Ex2(String s){
        super(s);
    }
}

//class for Exception3
class Ex3 extends RuntimeException
{
    Ex3(String s){
        super(s);
    }
}
class ExceptionDemo       //class to include main method
{
public static void main(String[] args ) {
    System.out.println("main begin");
    try {
        a();
    } catch (Ex1 ex) {
        System.out.println("main caught Ex1");
        throw new Ex3("exception3");
    } catch (Ex2 ex) {
       System.out.println("main caught Ex2");
    } catch (Ex3 ex) {
        System.out.println("main caught Ex3");
    } finally {
        System.out.println("main finally");
    }
    System.out.println("main end");
}

static void a( ) {
   System.out.println("a begin");
    try {
        b();
    } catch (Ex2 ex) {
        System.out.println("a caught Ex2");
    } catch (Ex3 ex) {
       System.out.println("a caught Ex3");
    }
    System.out.println("a end");
}

static void b( ) {
    boolean q1,q2,q3;
    System.out.println("b begin");
    q1=true;
    q2=false;
    q3=false;
    try {
        if (q1 == true) throw new Ex1("e1");
        if (q2 == true) throw new Ex2("e2");
        if (q3 == true) throw new Ex3("e3");
    } catch (Ex3 ex) {
        System.out.println("b caught Ex3");
    } finally {
        System.out.println("b finally");
    }
    System.out.println("b end");
}
}

output for q1=true and all other false

sh-4.3$ javac ExceptionDemo.java                                                                                              

sh-4.3$ java ExceptionDemo                                                                                                    

main begin                                                                                                                    

a begin                                                                                                                       

b begin                                                                                                                       

b finally                                                                                                                     

main caught Ex1                                                                                                               

main finally                                                                                                                  

main end

output for q2=true and all other false

sh-4.3$ javac ExceptionDemo.java                                                                                              

sh-4.3$ java ExceptionDemo                                                                                                    

main begin                                                                                                                    

a begin                                                                                                                       

b begin                                                                                                                       

b finally                                                                                                                     

a caught Ex2                                                                                                                  

a end                                                                                                                         

main finally                                                                                                                  

main end

output for q3=true and all other false

h-4.3$ javac ExceptionDemo.java                                                                                              

sh-4.3$ java ExceptionDemo                                                                                                    

main begin                                                                                                                    

a begin                                                                                                                       

b begin                                                                                                                       

b caught Ex3                                                                                                                  

b finally                                                                                                                     

b end                                                                                                                         

a end                                                                                                                         

main finally                                                                                                                  

main end  

To make exception 1 as checked exception, we can include conditions like assigning stirng to null ,division by zero condtioons etc.

instead of throwing an exception1 if (q1=true)

we can change like a=10,b=0

try{

c=a/b;

...

..

}

catch(ArithmeticException e)

{System.out.println(e);

}