Wednesday, August 8, 2012

Exception Handling Basics

Assume that processor refers to an object that provides a void method named process that takes no arguments. As it happens, the process method may throw one of several exceptions. Write some code that invokes the process method provided by the object associated with processor and arrange matters so that if process throws any exception, your code prints the message "process failure" to standard output and does nothing else in regard to the exception.

try {
processor.process();
}
catch(Exception e) {
System.out.println("process failure");
}

*************************
Assume that processor refers to an object that provides a void method named process that takes no arguments. As it happens, the process method may throw one of several exceptions. Write some code that invokes the process method provided by the object associated with processor and arrange matters so that your code causes any exception thrown by process to be ignored.

try {
processor.process();
}
catch(Exception e) {
// just ignore the exception
}

****************************


Assume you have class AutoFactory with two static methods called shutdown and reset. Neither method has any parameters. The shutdown; method may throw a ProductionInProgressException.

Write some code that invokes the shutdown method. If a ProductionInProgressException is thrown, your code should then invoke the reset method.

try {
AutoFactory.shutdown();
} catch (ProductionInProgressException pipe) {AutoFactory.reset();}

**************************
Assume that command is a variable of type String. Write a statement that throws an Exception with a message "null command" if command is null and that throws an Exception with a message "empty command" if command equals the empty string.

if (command==null)
throw new Exception("null command");
else if (command.equals(""))
throw new Exception("empty command");
**********************************

Write a statement that throws a newly created Exception object whose message is "does not compute".

throw new Exception("does not compute");

*******************************
Assume that dataFailure is a variable of type Exception that has been declared and that references an Exception object. Write a statement that throws that Exception object.

throw dataFailure;

******************************
Assume that command is a variable of type String. Write a statement that throws an Exception with a message "null command" if command is null.

if (command==null)
throw new Exception("null command");

***************************************
Write a statement that declares a reference variable e suitable for holding a reference to an Exception object.

Exception e;

********************************************
Write an expression whose value is a reference to a newly created Exception object whose message is "out of oil".

new Exception("out of oil")

********************************************
Write an expression whose value is a reference to a newly created Exception object with no specific message.

new Exception()
************************************

Write a statement that declares a reference variable abend suitable for holding a reference to an Exception object and initialize it to an Exception object whose message is "ABEND 013".

Exception abend = new Exception("ABEND 013");

********************************************
Assume that maxtries is a variable of type int that has been declared and given a value. Write an expression whose value is a reference to a newly created Exception object whose message is the string "attempt limit exceeded: " followed by the value of maxtries.

new Exception("attempt limit exceeded: "+maxtries)

**********************************************

Assume that e is a variable of type Exception that has been declared and that references an Exception object. Write a statement that displays to standard output the message associated with e

System.out.println(e.getMessage());

******************************************
Assume that e is a variable of type Exception that has been declared and that references an Exception object. Assume that msg is a variable of type String. Write a statement that assigns to msg the message associated with e.

msg = e.getMessage();


***************************************************


Assume that validData is a variable of type boolean that has been declared. Assume that processor refers to an object that provides a void method named process that takes no arguments. As it happens, the process method may throw one of several exceptions, one of which is NumberFormatException. Write some code that invokes the process method provided by the object associated with processor and arrange matters so that any NumberFormatException that is thrown is caught and causes validData to be set to false.


try {
processor.process();
}
catch(NumberFormatException nfe) {
validData=false;
}
*******************************************

Assume that success is a variable of type boolean that has been declared. Assume that processor refers to an object that provides a void method named process that takes no arguments. As it happens, the process method may throw one of several exceptions. Write some code that invokes the process method provided by the object associated with processor and arrange matters so that if any exception is thrown success to be set to false but otherwise it is set to true.

success = true;
try {
processor.process();
}
catch (Exception e) {
success = false;
}

**********************************************
The 'parseInt' method of the 'Integer' class throws a 'NumberFormatException' when it is passed a String argument that it cannot convert to an int. Given the String variable s (which has already been assigned a value), write the code needed to convert the value in s assigning the result to the integer variable i

If a NumberFormatException is thrown, i should be assigned the value -1.

try {
i = Integer.parseInt(s);
} catch (NumberFormatException e) { i = -1;}

*****************************
Write a statement that throws an IOException with the customized message "really BAD data".

throw new IOException("really BAD data");



Write the definition of a class named PanicException that has no associated message (i.e. null).

public class PanicException extends Exception {
}
******************************
Write the definition of a class named PanicException that supports the specification of a message. In addition, this version of PanicException arranges for the string "PANIC! " appears before the message. And if a PanicException is instantiated without an argument, its associated message will simply be "PANIC!".

public class PanicException extends Exception {
public PanicException() {
super("PANIC!");
}
public PanicException(String msg) {
super("PANIC! "+msg);
}
}
************************************
The argument to a square root method (sqrt) in general should not be negative. Write the definition of a class named SQRTException whose objects are created by specifying the original negative value that caused the problem in the first place: new SQRTException(originalArgument). The associated message for this Exception should be "Bad argument to sqrt: " followed by the value specified at the time of instantiation.

public class SQRTException extends Exception {
public SQRTException(double badvalue) {
super("Bad argument to sqrt: " + badvalue);
}
}
****************************************
Assume that processor refers to an object that provides a void method named process that takes no arguments. As it happens, the process method may throw one of several exceptions. One of these is NumberFormatException, another is FileNotFoundException. And there are others. Write some code that invokes the process method provided by the object associated with processor and arranges matters so that if a NumberFormatException is thrown, the message "Bad Data" is printed to standard output, and if FileNotFoundException. is thrown, the message "Data File Missing" is printed to standard output. If some other Exception is thrown, its associated message is printed out to standard output.

try {
processor.process();
}
catch(NumberFormatException nfe) {
System.out.println("Bad Data");
}
catch(FileNotFoundException fnfe) {
System.out.println("Data File Missing");
}
catch(Exception e) {
System.out.println(e.getMessage());
}

********************************************



No comments:

Post a Comment