SCJP : exception
Mock questions for this chapter: at bottom of this page
Recognize the effect of an exception arising at a specified point in a code fragment. Note that the exception may
be a runtime exception, a checked exception, or an error.
Error
An Error is a subclass of Throwable that indicates serious problems that
a reasonable application should not try to catch. Most such errors are abnormal conditions. The
ThreadDeath error, though a "normal" condition, is also a subclass of
Error because most applications should not try to catch it.
A method is not required to declare in its throws clause any subclasses of
Error that might be thrown during the execution of the method but not caught,
since these errors are abnormal conditions that should never occur.
java.lang.Object
java.lang.Throwable
java.lang.Error
Exception
The class Exception and its subclasses are a form of Throwable that
indicates conditions that a reasonable application might want to catch.
java.lang.Object
java.lang.Throwable
java.lang.Exception
RuntimeException
RuntimeException is the superclass of those exceptions that can be thrown during the
normal operation of the Java Virtual Machine.
A method is not required to declare in its throws clause any subclasses of
RuntimeException that might be thrown during the execution of the method but
not caught.
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
ArrayIndexOutOfBoundsException
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater
than or equal to the size of the array.
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.IndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
ClassCastException
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
For example, the following code generates a ClassCastException:
Object x = new Integer(0);
System.out.println((String)x);
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.ClassCastException
IllegalArgumentException
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.IllegalArgumentException
IllegalStateException
Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or
Java application is not in an appropriate state for the requested operation.
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.IllegalStateException
NullPointerException
Thrown when an application attempts to use null in a case where an object is required.
These include:
Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other illegal uses of the
null object.
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.NullPointerException
NumberFormatException
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the
string does not have the appropriate format.
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.IllegalArgumentException
java.lang.NumberFormatException
AssertionError
Thrown to indicate that an assertion has failed. The seven one-argument public constructors provided by this class
ensure that the assertion error returned by the invocation:
new AssertionError(expression)
java.lang.Object
java.lang.Throwable
java.lang.Error
java.lang.AssertionError
ExceptionInInitializerError
Signals that an unexpected exception has occurred in a static initializer. An
ExceptionInInitializerError is thrown to indicate that an exception occurred during
evaluation of a static initializer or the initializer for a static variable.
As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining
mechanism. The "saved throwable object" that may be provided at construction time and accessed via
the getException() method is now known as the cause, and may be accessed via
the Throwable.getCause() method, as well as the aforementioned "legacy method."
java.lang.Object
java.lang.Throwable
java.lang.Error
java.lang.LinkageError
java.lang.ExceptionInInitializerError
StackOverflowError
Thrown when a stack overflow occurs because an application recurses too deeply.
java.lang.Object
java.lang.Throwable
java.lang.Error
java.lang.VirtualMachineError
java.lang.StackOverflowError
NoClassDefFoundError
Thrown if the Java Virtual Machine or a ClassLoader instance tries to load
in the definition of a class (as part of a normal method call or as part of creating a new instance
using the new expression) and no definition of the class could be found.
The searched-for class definition existed when the currently executing class was compiled, but
the definition can no longer be found.
java.lang.Object
java.lang.Throwable
java.lang.Error
java.lang.LinkageError
java.lang.NoClassDefFoundError
SCJP 1.5 SCJP 1.6 Inner classes Mock Exam Practice Questions
Questions no -1
What is the output for the below code ?
public class SuperClass {
public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
String signature = "(String, Integer[])";
System.out.println(str + " " + signature);
return 1;
}
}
public class SubClass extends SuperClass{
public int doIt(String str, Integer... data) throws Exception
{
String signature = "(String, Integer[])";
System.out.println("Overridden: " + str + " " + signature);
return 0;
}
public static void main(String... args)
{
SuperClass sb = new SubClass();
try{
sb.doIt("hello", 3);
}catch(Exception e){
}
}
}
options
A)Overridden: hello (String, Integer[])
B)hello (String, Integer[])
C)This code throws an Exception at Runtime
D)Compile with error
Correct answer is : D
Explanations : Exception Exception is not compatible with throws clause in SuperClass.doIt(String, Integer[]).
The same exception or subclass of that exception is allowed.
Questions no -2
What is the result of executing the following code, using the parameters 0 and 3 ?
public void divide(int a, int b) {
try {
int c = a / b;
} catch (Exception e) {
System.out.print("Exception ");
} finally {
System.out.println("Finally");
}
options
A)Prints out: Exception Finally
B)Prints out: Finally
C)Prints out: Exception
D)Compile with error
Correct answer is : D
Explanations : finally block always executed whether exception occurs or not.
0/3 = 0 Does not throws exception.
|