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

JAVA 5. Name three exception handling techniques. Explain and provide examples.

ID: 3676523 • Letter: J

Question

JAVA

5. Name three exception handling techniques. Explain and provide examples.(6 points)

6. What is the output of the following java code? Explain your answer. (5 points)

   final double PI = 3.14159;
   double[] cylinderRadii = {3.5, 7.2, 10.5, 9.8, 6.5};
   double[] cylinderHeights = {10.7, 6.5, 12.0, 10.5, 8.0};
   double[] cylinderVolumes = new double[5];
  
   for(int i = 0; i < 5; i++)
       cylinderVolumes[i] = 2 * PI * cylinderRadii[i]

                        * cylinderHeights[i];
                         
        
   for (int i = 0; i < 5; i++)
       System.out.printf("%d %5.2f %5.2f %7.2f %n",
           (i + 1), cylinderRadii[i], cylinderHeights[i],                                                 

cylinderVolumes[i]);

Explanation / Answer

5)

a. Try Catch in Java – Exception handling
The try block contains a block of program statements within which an exception might occur. A try block is always followed by a catch block, which handles the exception that occurs in associated try block. A try block must followed by a Catch block or Finally block or both.
Syntax of try block

try{
   //statements that may cause an exception
}

A catch block must be associated with a try block. The corresponding catch block executes if an exception of a particular type occurs within the try block. For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception executes.


Syntax of try catch in java

try
{
     //statements that may cause an exception
}
catch (exception(type) e(object))
{
     //error handling code
}

An example of Try catch in Java

class Example1 {
   public static void main(String args[]) {
     int num1, num2;
     try {
        // Try block to handle code that may cause exception
        num1 = 0;
        num2 = 62 / num1;
        System.out.println("Try block message");
     } catch (ArithmeticException e) {
            // This block is to catch divide-by-zero error
            System.out.println("Error: Don't divide a number by zero");
       }
     System.out.println("I'm out of try-catch block in Java.");
   }
}

b. Using try/catch/finally blocks:

If exception occurs in try block’s body then control immediately transferred(skipping rest of the statements in try block) to the catch block. Once catch block finished execution then finally block and after that rest of the program.
    If there is no exception occurred in the code which is present in try block then first, the try block gets executed completely and then control gets transferred to finally block (skipping catch blocks).
    If a return statement is encountered either in try or catch block. In such case also finally runs. Control first goes to finally and then it returned back to return statement.

Consider the below example to understand above mentioned points:

class TestExceptions {
   static void myMethod(int testnum) throws Exception {
      System.out.println ("start - myMethod");
      if (testnum == 12)
         throw new Exception();
      System.out.println("end - myMethod");
      return;  
   }
   public static void main(String args[]) {
      int testnum = 12;
      try {
         System.out.println("try - first statement");
         myMethod(testnum);
         System.out.println("try - last statement");
      }
      catch ( Exception ex) {
         System.out.println("An Exception");
      }
      finally {
         System. out. println( "finally") ;
      }
      System.out.println("Out of try/catch/finally - statement");
   }
}

Output:

try - first statement
start - myMethod
An Exception
finally
Out of try/catch/finally - statement

c.   throw exception
In java we have already defined exception classes such as ArithmeticException, ArrayIndexOutOfBoundsException, NullPointerException etc. There are certain conditions defined for these exceptions and on the occurrence of those conditions they are implicitly thrown by JVM(java virtual machine).
Syntax of throw statement

throw AnyThrowableInstance;

Example:

//A void method
public void sample()
{
   //Statements
   //if (somethingWrong) then
   IOException e = new IOException();
   throw e;
   //More Statements
}

6).

Output:

1 3.50 10.70 235.31
2 7.20 6.50 294.05
3 10.50 12.00 791.68
4 9.80 10.50 646.54
5 6.50 8.00 326.73

I have commented each line of code. Please go through each comment .

// storing value of PI
       final double PI = 3.14159;
       // this is array that stores radius of 5 cylinder
           double[] cylinderRadii = {3.5, 7.2, 10.5, 9.8, 6.5};
           // this is array that stores height of corresponding cylinders
           double[] cylinderHeights = {10.7, 6.5, 12.0, 10.5, 8.0};
         
           // this is array to store volume of cylinders
           double[] cylinderVolumes = new double[5];
      
           // iterating through each cylinder , using radius and height of corresponding cylinder
           // calculating voulme and storing in array
           for(int i = 0; i < 5; i++)
               cylinderVolumes[i] = 2 * PI * cylinderRadii[i]* cylinderHeights[i];// volume calculation
                               
           
           // displaying radius, height and volume upto two decimal digits
           for (int i = 0; i < 5; i++)
               System.out.printf("%d %5.2f %5.2f %7.2f %n",
                   (i + 1), cylinderRadii[i], cylinderHeights[i], cylinderVolumes[i]);