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

i want the answer for the question. i do not need the code in java program. Lab

ID: 3709819 • Letter: I

Question

i want the answer for the question. i do not need the code in java program.

Lab Report Questions:

1-Describe what an exception does.

2-Describe the order in which a series of “catches” need to be.

Objective:

Write two classes:  One class draws a square, and an exception that occurs when the user puts in an invalid dimension.

First download the driver and put it in your project

DO NOT ALTER THE DRIVER!

Write a class file called DimensionException that inherits from Exception

Create the following constructors

Default – calls the parent’s constructor and pass a message that it is an invalid dimension

A constructor that takes one string parameters that is passed to the parent’s constructor

Write a class file called Square that DOES NOT HAVE  a main method

An attribute of this class is

Length – corresponding to the length of each side

Create the following constructors

Default – creates a 1x1 square (or a single star)

One that has a integer parameter that corresponds to its length and width

Accessors and Mutators for each variable

MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!

If the dimension is set improperly throw the DimensionException

Create the following Methods

draw – this method will draw a square of asterisks (*) by the given dimensions.  

getArea – returns the area of the square

getPerimeter – returns the perimeter of the square

Example Dialog:

Welcome to the easy square program

Enter the length of the side of a square or enter QUIT to quit

5

*****

*****

*****

*****

*****

The area is 25

The perimeter is 20

Enter the length of the side of a square or enter QUIT to quit

0

Dimensions must be of length one at least

Enter the length of the side of a square or enter QUIT to quit

Lab Report Questions:

Describe what an exception does.

Describe the order in which a series of “catches” need to be.

Explanation / Answer

NOT ANSWERING THE PROGRAMMING PART IN JAVA AS PER STATED IN THE QUESTION!!!

Answers of Lab Report Questions :

1) Exception, or Exceptional event is an unexpected or unwanted event which occurs during the execution of a program, that doesn't let normal flow of the program's instructions. When any error occurs within a method, the method creates a object and hands it off to the runtime system. The exception object, which is how it's called, contains all the information about the error including it's type and the state of the program when the unexpected error occurred. Creating an exception object and handing it to the runtime system is also called as throwing an exception. This runtime system in Java Programming language is also called the Java Virtual Machine(JVM).

All exceptions and error types in Java are the subclasses of the parent class Throwable. After a method throws an exception, the runtime system or JVM tries to find something that will be able to handle this error or exception. The set of all possible handlers of this exception is the ordered list of methods that had been called to get to the method where the error actually occurred. This list of methods is known as the Call Stack.

The runtime system searches the call stack for a block of code in a method that can handle the exception. This block of code is also called the exception handler. The search for the exception handler starts from the method where the error occurred and continues through the call stack in the reverse order in which the methods were called. When an appropriate handler is found, the runtime system passes the exception to the appropriate exception handler, which is deemed appropriate if the type of the exception object thrown matches the type that can be handled by the exception handler.

The exception handler then catches the exception. If the runtime system searches the entire call stack and doesn't find an appropriate exception handler, the runtime system as well as the program terminates.

2) The order of the catch statements that should be maintained is, whatever catch statement matches first, gets executed. If the first catch statement matches the exception, it is executed. If it doesn't then the next catch statement is tried and it goes on until a matching catch statement is executed or none of them are eventually.

The reason a order should be maintained to execute catch statements to catch a possible exception is because it's a compilation error to catch a generic or parent exception first and then one of it's descendants or child exception later. For example -

try {

// some code here

} catch (Exception e) {

e.printStackTrace();

} catch (ArrayIndexOutOfBoundsException e) {

e.printStackTrace();

}

In this example, the ArrayIndexOutOfBoundsException catch block will cause a compilation error because it will never be executed as the first catch block will handle any kind of exception as it's a generic or parent exception handler.

So, when catching exceptions, you should always catch the most specific exception, and then the most generic exceptions like RuntimeException or Exception.

For example, suppose you want to catch the ArrayIndexOutOfBoundsException but your code could also throw a NullPointerException, you should follow the below procedure to catch the exceptions -

int[] array = new int[5];

try {

int n = array[10]; // will throw an error since array is declared as size 5 but we want to access it's 10th index

} catch (NullPointerException e) {

System.out.println(e);

e.printStacktrace();

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println(e);

e.printStackTrace();

} catch (RuntimeException e) {

System.out.println(e);

e.printStacktrace();

} catch (Exception e) {

System.out.println(e);

e.printStacktrace();

}

So, with this order it's ensured that all the exceptions are caught correctly. If it's a NullPointerException it enters the first catch block, if it's a ArrayIndexOutOfBounds Exception it enters the second catch block and if it's RuntimeException it enters the third catch block and at last if it's something else then it enters the fourth and last catch block. So, you should always have the child exception handlers first and then the parent handlers.