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

public class CallStack{ // Called by func1() void func2 (){ System.out.println(\

ID: 3822975 • Letter: P

Question

public class CallStack{

// Called by func1()
void func2 (){
   System.out.println("In func2 method");
    int a = 0;
    int b;
    b = 10 / a;
}

//Called by Main
void func1(){
   System.out.println("In func1 method");
   this.func2 ();
   System.out.println("Back in func1 method");

}


public static void main (String args[]){
CallStack myCallStack;
myCallStack = new CallStack();
System.out.println("In the main method");
myCallStack.func1 ();

}
}

Handling exceptions a. Download the following file from the class website: CallStack.java b. Examine the code to determine What it does. c. Compile and execute the code. d. Modify the main0 method to handle the exception that is propagated to it. Use a try catch block to display a meaningful error message when the exception occurs e. Test your code. Notice that, although the exception was thrown in func2, it is caught by the catch block in the main method.

Explanation / Answer

package org.students;

public class CallStack {
   // Called by func1()
   void func2 (){
   System.out.println("In func2 method");
   int a = 0;
   int b;
   if(a==0)

//Here we are creating exception and throwing out of the func2() function
   throw new IllegalArgumentException("Invalid.Divided by zero");
   else
       b = 10 / a;  
   }
   //Called by Main
   void func1(){
   System.out.println("In func1 method");
   try {
       this.func2 ();          
       } catch (Exception e) {
   System.out.println(e);
       }

   System.out.println("Back in func1 method");
   }

   public static void main (String args[]){
   CallStack myCallStack;
   myCallStack = new CallStack();
   System.out.println("In the main method");
   myCallStack.func1 ();
   }
}

__________________

Output:

In the main method
In func1 method
In func2 method
java.lang.IllegalArgumentException: Invalid.Divided by zero
Back in func1 method

______________Thank You