Java Program: You will generate at least eight (8) exceptions and catch them. Fo
ID: 3688240 • Letter: J
Question
Java Program:
You will generate at least eight (8) exceptions and catch them. For full points, two of these exceptions will have to be created by you. You will be required to implement both a NoogieException and a CoogieException (detailed below).
List of possible exceptions:
NoogieException (required)
CoogieException (required)
ArrayIndexOutOfBoundsException
NullPointerException
ClassCastException
ArithmeticException
NegativeArraySizeException
StringIndexOutOfBoundsException
NumberFormatException
ArrayStoreException
FileNotFoundException
NoSuchElementException
IOException
Any others you can think of or find!
Note that, aside from NoogieException and CoogieException, you must cause these exceptions "organically". That is, you cannot simply create a new IOException and throw it (detailed later). You must do something in normal Java which causes that exception to occur.
This lab will not tell you how to generate these exceptions - it is incumbent upon you to do the research. I hear there are some excellent search engines out there nowadays.
After causing one of these exceptions to occur, you should enclose it in a try..catch block, like so.
If an exception is thrown inside a try block, the corresponding catch block (which must be right after the try block) will "catch" the "thrown" exception. The exception will then not be thrown out of the method, and execution will continue in the same method (instead of the method ending, as it does if an exception is generated and is not caught, like we have been doing). This will allow us to fix any problems or show an error message before moving on. It also allows our program to recover from unexpected problems (e.g., dividing by zero or trying to read from a nonexistent file).
You should print out the String representation of the exception for each exception caused.
There will be numerous instances of these try..catch blocks. Each try..catch block should be indepedent; that is, the first exception should occur, and then caught, then a new try..catch block starts. The try..catch blocks should not be nested.
You are required to create two of your own kinds of exceptions. These both should be subclasses of class Exception. They can be specified in the same file as your main class.
NoogieException will not override any methods. You can then force the code to throw the exception by using the throwkeyword. An Exception is an object, so you must create it with the new keyword first, and then throw it. For example:
CoogieException will allow the user to enter one argument, an int, in the constructor. The relevant parameter will be named numCats. CoogieException will also override the .toString() method. .toString() will return the string `numCats + " is too many cats!".
The final output of the program should just be a list of all the exceptions that were caught in the catch blocks.
Explanation / Answer
class Test{ public static void main(String[] args) throws IOException { int[] arr = {1,2,3,4,5}; try { throw (new NoogieException()); } catch (NoogieException ex) { System.out.println(ex.toString()); } try { throw (new CoogieException(23)); } catch (CoogieException ex) { System.out.println(ex.toString()); } try{ System.out.println(arr[10]); }catch (ArrayIndexOutOfBoundsException e){ System.out.println(e.toString()); } int[] nE = null; try{ System.out.println(nE.length); }catch (NullPointerException e){ System.out.println(e.toString()); } try{ Object x = new Integer(0); System.out.println((String)x); }catch (ClassCastException e){ System.out.println(e.toString()); } try{ int num1=30, num2=0; int output=num1/num2; System.out.println ("Result = " +output); } catch(ArithmeticException e) { System.out.println(e.toString()); } try{ int i = -1; int[] array = new int[i]; }catch (NegativeArraySizeException e){ System.out.println(e.toString()); } try{ Object x[] = new String[3]; x[0] = new Integer(0); }catch (ArrayStoreException e){ System.out.println(e.toString()); } try{ File f = new File("invalidefile.xyz"); }catch (ArrayStoreException e){ System.out.println(e.toString()); } InputStream input = null; try{ input = new FileInputStream("c:\data\input-text.txt"); int data = input.read(); while(data != -1) { //do something with data... data = input.read(); } }catch(IOException e){ System.out.println(e.toString()); } finally { if(input != null) input.close(); } Set sampleSet = new HashSet(); try{ sampleSet.iterator().next(); }catch (NoSuchElementException e){ System.out.println(e.toString()); } try{ int num=Integer.parseInt ("XYZ") ; System.out.println(num); }catch(NumberFormatException e) { System.out.println(e.toString()); } try{ String str="easysteps2buildwebsite"; System.out.println(str.length());; char c = str.charAt(0); c = str.charAt(40); System.out.println(c); }catch(StringIndexOutOfBoundsException e) { System.out.println(e.toString()); } } } class NoogieException extends Exception{ public NoogieException() { } } class CoogieException extends Exception{ int numCats; public CoogieException(int numCats) { this.numCats = numCats; } @Override public String toString() { return numCats + "is too many cats!"; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.