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

Exception Handling 1. Design and create a Java application that generates, detec

ID: 3887917 • Letter: E

Question

Exception Handling

1. Design and create a Java application that generates, detects, and handles the following exceptions. Test each scenario with ‘good’ and ‘bad’ data:

a.IndexOutOfBoundsException

b.FileNotFoundException

c.NullPointerException

d.NumberFormatException

e.MyLastNameIsSpelledWrongException – you will need to create this exception class and generate (throw) the error yourself for this one.
Example for e.
public class MyTestException extends Exception {

public MyTestException()

{

}

public MyTestException(String msg)

{

super(msg);

}

}

*please leave good comments and just make a,b,c,d,e into seperate programs so it is easier to test with good and bad data.

Explanation / Answer

I created seperate driver program for every exception class

/****************************NumberFormatExceptionDriver.java*********************/

/**

* The Class NumberFormatExceptionDriver.

*/

public class NumberFormatExceptionDriver {

/**

* The main method.

*

* @param args the arguments

*/

public static void main(String[] args) {

try {

// NumberFormatException testing

String s = "a";

int number = Integer.parseInt(s);

} catch (NumberFormatException e) {

System.out.println("NumberFormatException occured");

}

}

}

/***********************output************************/

NumberFormatException occured

/**************************************NullPointerExceptionDriver.java***********************/

/**

* The Class NullPointerExceptionDriver.

*/

public class NullPointerExceptionDriver {

/**

* The main method.

*

* @param args the arguments

*/

public static void main(String[] args) {

try {

// NullPointerException Testing

String str = null;

String arr[] = str.split("");

} catch (NullPointerException e) {

System.out.println("NullPointerException occured");

}

}

}

/**************************output***************************/

NullPointerException occured

/*********************************FileNotFoundExceptionDriver.java*************************************/

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.Scanner;

/**

* The Class FileNotFoundExceptionDriver.

*/

public class FileNotFoundExceptionDriver {

/**

* The main method.

*

* @param args the arguments

*/

public static void main(String[] args) {

final String filename = "xyz.txt";

BufferedReader rd = null;

Scanner input = new Scanner(System.in);

try {

// Testing FileNotFoundException

// Open the file for reading.

rd = new BufferedReader(new FileReader(new File(filename)));

} catch (FileNotFoundException ex) {

System.out.println("FileNotFoundException occured");

}

}

}

/*******************output*************************/

FileNotFoundException occured

/*******************************MyLastNameIsSpelledWrongException.java***********************/

/**

* The Class MyLastNameIsSpelledWrongException.

*/

public class MyLastNameIsSpelledWrongException extends Exception {

/**

* GENERATED.

*/

private static final long serialVersionUID = -6219423655582088902L;

/**

* Instantiates a new my last name is spelled wrong exception.

*/

public MyLastNameIsSpelledWrongException() {

super();

}

/**

* Instantiates a new my last name is spelled wrong exception.

*

* @param msg the msg

*/

MyLastNameIsSpelledWrongException(String msg) {

super(msg);

}

}

/*********************************MyLastNameIsSpelledWrongExceptionDriver.java*********************************/

import java.util.Scanner;

/**

* The Class MyLastNameIsSpelledWrongExceptionDriver.

*/

public class MyLastNameIsSpelledWrongExceptionDriver {

/**

* The main method.

*

* @param args the arguments

*/

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

try {

// MyLastNameIsSpelledWrongException Testing

System.out.println("Please enter your last name");

String lname = input.nextLine();

if (!"mali".equals(lname)) {

throw new MyLastNameIsSpelledWrongException("My Last name is incorrect");

}

} catch (MyLastNameIsSpelledWrongException e) {

System.out.println("MyLastNameIsSpelledWrongException occured");

}

}

}

/*************************output**************************/

Please enter your last name
saini
MyLastNameIsSpelledWrongException occured

/******************************IndexOutOfBoundsExceptionDriver.java************************************/

/**

* The Class IndexOutOfBoundsExceptionDriver.

*/

public class IndexOutOfBoundsExceptionDriver {

/**

* The main method.

*

* @param args the arguments

*/

public static void main(String[] args) {

try {

int arr[] = new int[2];

System.out.println(arr[3]);

} catch (IndexOutOfBoundsException ex) {

System.out.println("IndexOutOfBoundsException occured");

}

}

}

/**************************output*****************************/

IndexOutOfBoundsException occured

Thanks a lot. Please let me know if you have any doubt.

/***********************Single program**********************/

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.Scanner;

public class ExceptionDriver {

public static void main(String[] args) {

final String filename = "xyz.txt";

BufferedReader rd = null;

Scanner input = new Scanner(System.in);

try {

// Testing FileNotFoundException

// Open the file for reading.

rd = new BufferedReader(new FileReader(new File(filename)));

// NullPointerException Testing

String str = null;

String arr[] = str.split("");

// NumberFormatException testing

String s = "a";

int number = Integer.parseInt(s);

// MyLastNameIsSpelledWrongException Testing

System.out.println("Please enter your last name");

String lname = input.nextLine();

if (!"mali".equals(lname)) {

throw new MyLastNameIsSpelledWrongException("My Last name is incorrect");

}

} catch (IndexOutOfBoundsException ex) {

System.out.println("IndexOutOfBoundsException occured");

} catch (FileNotFoundException ex) {

System.out.println("FileNotFoundException occured");

} catch (NullPointerException e) {

System.out.println("NullPointerException occured");

} catch (NumberFormatException e) {

System.out.println("NumberFormatException occured");

} catch (MyLastNameIsSpelledWrongException e) {

System.out.println("MyLastNameIsSpelledWrongException occured");

}

}

}