Question

In: Computer Science

This programming assignment involves learning about some of the common exceptions that occur in Java programs.

This programming assignment involves learning about some of the common exceptions that occur in Java programs. Consider the following exception types: NullPointerException ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException Research what each exception type means and the conditions under which each occurs (i.e., is thrown). Write programs that demonstrate each type of exception being thrown (one program per exception) and provide a screen capture of the output. You should write your code so that each exception type is forced to occur. Name your programs as follows: NullPointerExceptionThrown, etc. Then, write a program that catches each type of thrown exception and display an error message indicating some of the details as to what that exception is. Write one program per type of exception. Name your programs as follows: NullPointerExceptionCatch, etc.

Solutions

Expert Solution

The problem of accessing an array element beyond the size of an array is represented by a class called ArrayIndexOutOfBoundsException. Let’s take an example- suppose marks of 20 students are stored in an array. A particular student marks are known by taking the roll number (it is index number) from the keyboard. The array index is from 0 to 19. If the user enters 20, there is no element in an array with index 20. JVM terminates the execution. Before terminating, it gives a proper message of the problem. This example is like this mentioned example. The index is permitted 0 to 2 only but the user is giving 3, it is beyond the array size.

import java.io.*;
public class ArrayIndexOutofBoundThrown
{
public static void main(String args[])
{
int marks[] = { 40, 50, 60 };
System.out.println("Hello 1");

int m1 = marks[3];
System.out.println("Marks are " + m1);

System.out.println("Hello 2");
System.out.println("Hello 3");
}
}

NullPointerException indicates, if an object points to null and further used in the code, the JVM throws NullPointerException. In the following program, string str is assigned with Null. The Null variable str is compared with “Hello”.The JVM reacts by throwing NullPointerException.

import java.io.*;
public class NullPointerExceptionThrown
{
public static void main(String args[])
{
String str = null;
System.out.println(str.equals("hello"));
}
}

As the name indicates, this exception is raised by JVM when it is unable to cast two objects which are done in the program by the programmer against the rules of java casting. In this program, test object f is explicitly cast to ClassCastExceptionThrow and assigned. It compiles, but at execution time throws ClassCastException. Why did it raise? The rule says “before doing explicit casting, there must be implicit casting done earlier”. But in this code, it is not done.

import java.io.*;
class test {
}
public class ClassCastExceptionThrown extends test
{
public static void main(String args[])
{
test f = new test();;
ClassCastExceptionThrown r = new ClassCastExceptionThrown();
              
// f = r;

r = (ClassCastExceptionThrown) f;
}
}

It is thrown by Color constructor when wrong parameters are passed. The RGB values should be within the range of 0 to 255. If other values are passed Color constructor throws IllegalArgumentException.

import java.awt.*;
public class IllegalArgumentExceptionThrown
{
public static void main(String args[])
{
Color clr1 = new Color(300, 150, 200);
}
}


Related Solutions

This programming assignment involves learning about some common exceptions which occur in Java programs. Consider the...
This programming assignment involves learning about some common exceptions which occur in Java programs. Consider the following exception types: NullPointerException ArrayIndexOutOfBounds Exception ClassCastException IllegalArgumentException Research what each exception type means and the conditions under which each occurs (thrown). Then write the following programs, one for each of the above-listed exception types: A program which throws the exception (with a throw statement) and catches it displaying unique information about the exception. Name your programs <exception>Thrown.java <exception> is the name of the...
*OBJECT ORIENTED PROGRAMMING* JAVA PROGRAMMING GOAL: will be able to throw and catch exceptions and create...
*OBJECT ORIENTED PROGRAMMING* JAVA PROGRAMMING GOAL: will be able to throw and catch exceptions and create multi-threaded programs. Part I Create a class called Animal that implements the Runnable interface. In the main method create 2 instances of the Animal class, one called rabbit and one called turtle. Make them "user" threads, as opposed to daemon threads. Some detail about the Animal class. It has instance variables, name, position, speed, and restMax. It has a static boolean winner. It starts...
*OBJECT ORIENTED PROGRAMMING* GOAL: will be able to throw and catch exceptions and create multi-threaded programs....
*OBJECT ORIENTED PROGRAMMING* GOAL: will be able to throw and catch exceptions and create multi-threaded programs. Part I Create a class called Animal that implements the Runnable interface. In the main method create 2 instances of the Animal class, one called rabbit and one called turtle. Make them "user" threads, as opposed to daemon threads. Some detail about the Animal class. It has instance variables, name, position, speed, and restMax. It has a static boolean winner. It starts a false....
What is the java root class for exceptions? java.lang.throw java.lang.exceptions java.lang.errors java.lang.Throwable Which statement about exceptions...
What is the java root class for exceptions? java.lang.throw java.lang.exceptions java.lang.errors java.lang.Throwable Which statement about exceptions is NOT true. Exception handling enables a program to deal with errors during execution. Exceptions are compile time errors. Exceptions are thrown from a method. An exception is an Exceptions can be primitives or objects. True False "try-with-resources" syntax is used to ___________________. automatically close the files used in the program. throw an exception for a memory related error. all of the above validate...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock , for example 23:47:55). Here are the input errors (Exceptions)...
Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT