Question: intro to java ( exceptions chapter) 1. Define an exception class called CoreBreachException. The class should have a default constructor. If an exception is thrown

intro to java ( exceptions chapter)

1. Define an exception class called CoreBreachException. The class should have a default constructor. If an exception is thrown using this zero-argument constructor, getMessage should return "Core Breach! Evacuate Ship!" The class should also define a constructor having a single parameter of type String. If an exception is thrown using this constructor, getMessage should return the value that was used as an argument to the constructor.

2.Repeat the previous question, but instead name the class MessageTooLongException. Also, if an exception is thrown using the default constructor, getMessage should return "Message Too Long!"

3. Suppose the exception class ExerciseException is defined as follows:

public class ExerciseException extends Exception { public ExerciseException() { super("Exercise exception thrown!"); System.out.println("Exception thrown."); } public ExerciseException(String message) { super(message); System.out.println("ExerciseException invoked "+ "with an argument."); } }

What output would be produced by the following unlikely code?

ExerciseException e = new ExerciseException("Do Be Do"); System.out.println(e.getMessage());

4.Suppose the exception class CrazyException is defined as follows:

public class CrazyException extends Exception { public CrazyException() { super("Crazy exception thrown!"); System.out.println("Wow, Crazy exception thrown!"); } public CrazyException(String message) { super(message); System.out.println("Wow, crazy exception thrown with "+ "an argument!"); } public void crazyMethod() { System.out.println("Message is " + getMessage()); } }

What output would be produced by the following unlikely code?

CrazyException exceptionObject = new CrazyException(); System.out.println(exceptionObject.getMessage()); exceptionObject.crazyMethod();

5.Suppose the exception class DoubleException is defined as follows:

public class DoubleException extends Exception { public DoubleException() { super("Double exception thrown!"); } public DoubleException(String message) { super(message + " " + message); } }

What output would be produced by the following unlikely code?

try { System.out.println("try block entered:"); int number = 42; if (number > 0) throw new DoubleException("DoubleException thrown!"); System.out.println("Leaving try block."); } catch(DoubleException exceptionObject) { System.out.println(exceptionObject.getMessage()); } System.out.println("End of code.");

6. Although an exception class normally carries only a string message, you can define exception classes to carry a message of any type. You can give your exception class instance variables and methods. For example, objects of the following type can also carry an int message, as well as a string message:

public class IntException extends Exception { private int intMessage; public IntException() { super("IntException thrown!"); } public IntException(String message) { super(message + " " + message); } public IntException(int number) { super("IntException thrown!"); intMessage = number; } public int getNumber() { returnintMessage; } }

What output would be produced by the following unlikely code?

IntException e = new IntException(42); System.out.println(e.getNumber()); System.out.println(e.getMessage());

7. Can you derive an exception class from the predefined class IOException, or must it be derived from the class Exception?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!