Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Phillip Guerra

Class: IFT210 – 25709


Topic: Week 6 Homework
Instructor: Rick Bird

Homework 6
Ex 11.2 What would happen if the try statement were removed from the level1 method of the
ExceptionScope class in the Propagation program?

If the try statement were removed from the level1 method, then an exception would be thrown
and produce an error message because it would move to the main method which has no try
statement.

Ex. 11.3 What would happen if the try statement described in the previous exercise were moved
to the level2 method?

The exception that is thrown in level3 would move to level2. It should be caught and handled
there in that level’s catch method and the level 2 method would be allowed to finish because it
was handled.

Ex 11.4 Look up the following exception classes in the online Java API documentation and
describe their purpose:

A. ArithmeticException - This exception is caused when an arithmetic error occurs, such as


dividing by zero.

B. NullPointerException - This exception is caused when attempting to call an instance method


of a null reference, but an object is required instead.

C. NumberFormatException - This exception is caused when a program tries to convert a string


into a numeric value when the string does not have the right format.

D. PatternSyntaxException - This exception is caused when a syntax error occurs in the pattern
of a regular expression.

PP 11.1 Write a program that creates an exception class called StringTooLongException,


designed to be thrown when a string is discovered that has too many characters in it. In the main
driver of the program, read strings from the user until the user enters "DONE". If a string is
entered that has too many characters (say 20), throw the exception. Allow the thrown exception
to terminate the program.

Exception class

//StringTooLongException make throwable


public class StringTooLongException extends Exception
{
public StringTooLongException()
{
System.out.println("The String is too long!");
//allow class to exit
System.exit(0);
}
}

Main driver program


//StringTooLongException program
import java.util.Scanner;

public class ReadStrings


{
public static void main(String[] args) throws StringTooLongException
{
Scanner scan = new Scanner(System.in);

//input
System.out.println("Enter a string, enter DONE when finished: ");
String input = scan.nextLine();

//exit if done
if(input.equalsIgnoreCase("DONE"))
System.exit(0);

do
{
try
{
//use throwable class
if(input.length() > 20)
throw new StringTooLongException();
}
finally
{
//repeat input if not too long or done
System.out.println("Enter another string, enter DONE when finished: ");
input = scan.nextLine();
}
}
while(!input.equalsIgnoreCase("DONE"));
scan.close();
}
}

PP 11.2 Modify the solution to PP 11.1 such that it catches and handles the exception if it is
thrown. Handle the exception by printing an appropriate message, and then continue processing
more strings.
Exception class

//StringTooLongException make throwable


public class StringTooLongException extends Exception
{
public StringTooLongException()
{
super("That string is too long!");
}
}

Main Driver Program


//StringTooLongException program
import java.util.Scanner;

public class ReadStrings


{
public static void main(String[] args) throws StringTooLongException
{
Scanner scan = new Scanner(System.in);

//input
System.out.println("Enter a string, enter DONE when finished: ");
String input = scan.nextLine();

//exit if done
if(input.equalsIgnoreCase("DONE"))
System.exit(0);

do
{
try
{
if(input.length() > 20)
throw new StringTooLongException();
System.out.println("Enter another string, enter DONE when finished: ");
input = scan.nextLine();
}
catch(StringTooLongException exception)
{
System.out.println(exception.getMessage());
System.out.println("Enter another string, enter DONE when finished: ");
input = scan.nextLine();
}
}
while(!input.equalsIgnoreCase("DONE"));
scan.close();
}
}

You might also like