Translate

Monday, January 7, 2013

Exception Handling Interview Questions


Q) Diff Exception & Error
 Exception and Error both are subclasses of the Throwable class.

ExceptionàException is generated by java runtime system (or) by manually. An exception is a abnormal condition that transfer program execution from a thrower to catcher.
Error        àWill stop the program execution, Error is a abnormal system condition we cannot handle these.

Q) Can an exception be rethrown?
A) Yes, an exception can be rethrown.

Q) try, catch, throw, throws
try         à This is used to fix up the error, to prevent the program from automatically terminating, try-catch is used to catching an exception that are thrown by the java runtime system.
Throw   à is used to throw an exception explicitly.
Throws à A Throws clause list the type of exceptions that a methods might through.

Q) What happens if an exception is not caught?
A) An uncaught exception results in the uncaughtException() method of the thread's ThreadGroup being invoked, which eventually results in the termination of the program in which it is thrown.

Q) What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement?
The exception propagates up to the next higher level try-catch statement (if any) or results in the program's termination.

Q) Checked & UnChecked Exception :-
Checked exception is some subclass of Exception. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method·
            
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method· Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.

Checked Exceptions
Un checked exception
ClassNotFoundException
ArithmeticException
NoSuchMethodException
ArrayIndexOutOfBoundException
NoSuchFieldException
ClasscastException
InterruptedException
IllegalArgumentException
IllegalAccessException
IllegalMonitorSateException
CloneNotSupportedException
IllegalThreadStateException

IndexOutOfBoundException

NullPointerException

NumberFormatException

StringIndexOutOfBounds

OutOfMemoryError --> Signals that JVM has run out of memory and that the garbage collector is unable to claim any more free memory.
StackOverFlow --> Signals that a stack O.F in the interpreter.
ArrayIndexOutOfbound --> For accessing an array element by providing an index values <0 or="or"> or equal to the array size.
StringIndexOutOfbound --> For accessing character of a string or string buffer with index values <0 or="or"> or equal to the array size.
Arithmetic Exception --> such as divide by zero.
ArrayStore Exception --> Assignment to an array element of an incompatible types.
ClasscastException --> Invalid casting.
IllegalArgument Exception --> Illegal argument is used to invoke a method.
Nullpointer Exception --> If attempt to made to use a null object.
NumberFormat Exception --> Invalid conversition of string to numeric format.
ClassNotfound Exception --> class not found.
Instantion Exception --> Attempt to create an object of an Abstract class or Interface.
NosuchField Exception --> A request field does not exist.
NosuchMethod Exception --> A request method does not exist.

Q) Methods in Exceptions?
A) getMessage(), toString(), printStackTrace(), getLocalizedMessage(),

Q) What is exception chaining?
A) An exception chain is a list of all the exceptions generated in response to a single root exception. As
each exception is caught and converted to a higher-level exception for rethrowing, it's added to the chain.
This provides a complete record of how an exception is handled The chained exception API was introduced in 1.4. Two methods and two constructors were added to Throwable.
Throwable getCause()
Throwable initCause(Throwable)
Throwable(String, Throwable)
Throwable(Throwable)

The Throwable argument to initCause and the Throwable constructors is the exception that caused the current exception. getCause returns the exception that caused the current exception, and initCause returns the current exception.

Q) Primitive multi tasking
            If the threads of different priorities shifting the control depend on the priority i.e.; a thread with higher priority is executed first than the thread with lower priority. This process of shifting control is known as primitive multi tasking.

Q) Http Status Codes
            To inform the client of a problem at the server end, you call the “sendError” method. This causes the server to respond with status line, with protocol version and a success or error code.
The first digit of the status code defines the class of response, while the last two digits do not have categories
Number
Type
Description
1xx
Informational
Requested received, continuing to process
2xx
Success
The action was successfully received, understood, and accepted
3xx
Redirection
Further action must be taken in order to complete the request
4xx
Client Error
The request contains bad syntax or cannot be fulfilled
5xx
Server Error
The server failed to fulfil valid request

No comments: