In: Computer Science
In-Class coding exercise: (try - catch) (pass / fail)
The purpose of this in-class coding exercise is to simulate various exception conditions using the program provided.
package com.InClass;
/*
* This program demonstrates the ability of catching different types
of exceptions"
* You will have to modify the method() codes to trigger
various/different exceptions.
* inClass Coding ( you will have to remove (comment out) the
previous error condition before moving to trigger the next)
* 1) throw a myExcept exception (this is defined with an extends to
Exception (see code)
* 2) use method () to trip a Arithmetic exception
* 3) use method () to trip a nullPointException
* 4) use method () call to trip a runtime exception
* 5) inside the myExcept error to inject an arithmetic error and
watch it behave differently from step1 22
*
*/
public class TestExceptions {
public static void main(String[] args) {
try {
// throw
myException() first.
method();
System.out.println(" after the method call");
}
catch (MyExcept ex){
System.out.println(" MyExcept error");
}
catch (ArithmeticException
ex){
System.out.println(" ArithmeticEx error");
}
catch (NullPointerException ex)
{
System.out.println(" nullPointException !!! ");
}
catch (IndexOutOfBoundsException e)
{
System.out.println(" index out of bounds...");
}
catch (RuntimeException ex )
{
System.out.println(" Runtime exception");
System.out.println(ex.getMessage());
}
catch (Exception e ) {
System.out.println(" Exception");
}
finally {
System.out.println(" finally After
the catch");
}
}
static void method() throws Exception {
// create ArithmeticException
// create RuntimeExceptiom
// create a null point exception here -- fix the exception above
first
// create a ClassCastException here which is NOT on catch "watch"
listed above
// throw new Exception(); // trigger an Exceptiom
}
}
class MyExcept extends Exception { // creating your custome
error
MyExcept(){
// int y = 1/0; // uncomment this
line to introduce an aritmetic exception inside your MyExcept
System.out.println(" inside my custome
MyExcept");
}
}
Please complete the missing exceptions. Ignore the spellings mistakes/grammer, it's how the professor writes.
Instructions might be a bit hard to follow so please bear that in mind. Complete as much as possible.
Thank you.
I have implemented all the comments in the method() function and also provided comments so that you can follow. You need to run the program several times running each exception at a time while commenting the rest. Hope this helps you, let me know for any questions or in case you have any follow up question. Thanks : ) ========================================================================
public class TestExceptions { public static void main(String[] args) { try { // when you want to run the below line please comment the next two lines // i.e comment method() and System.out.println(" after the method call"); throw new MyExcept(); // when you want to run the below 2 lines comment the above line i.e throw new MyExcept(); //method(); //System.out.println(" after the method call"); } catch (MyExcept ex) { System.out.println(" MyExcept error"); } catch (ArithmeticException ex) { System.out.println(" ArithmeticEx error"); } catch (NullPointerException ex) { System.out.println(" nullPointException !!! "); } catch (IndexOutOfBoundsException e) { System.out.println(" index out of bounds..."); } catch (RuntimeException ex) { System.out.println(" Runtime exception"); System.out.println(ex.getMessage()); } catch (Exception e) { System.out.println(" Exception"); } finally { System.out.println(" finally After the catch"); } } static void method() throws Exception { // create ArithmeticException // try to divide by zero; this will result in an Arithmetic Exception; int result = 5 / 0; // divide by zero will lead to an Arithmetic Exception; // create RuntimeException; // we can create an empty string and access the 5th character; String empty = ""; System.out.println("Try to print the 5th character: " + empty.charAt(4)); // create a null point exception here -- fix the exception above first // Create an array pointing to null and then try to access the 1st element int[] nullArray = null; System.out.println("1st element value: " + nullArray[0]); // create a ClassCastException here which is NOT on catch "watch" listed above Object s = new String(); Integer i = (Integer) s; // trying to cast a String to Integer class this will lead to ClassCastException // throw new Exception(); // trigger an Exceptiom throw new Exception("Exception"); } } class MyExcept extends Exception { // creating your custome error public MyExcept() { int y = 1/0; // uncomment this line to introduce an aritmetic exception inside your MyExcept System.out.println("Inside my custom MyExcept"); } }
==============================================================