Flow Control and exception
What is the difference between while and do while loop
Do while loop walways executes the body of the
loop at least once, since the test is performed at the end of the body
When do you use continue and when do you use break statements
When continue statement is applied it
prematurely completes the iteration of a loop.
When break statement is applied it causes the entire loop to be abandoned.
What is the base class from which all exceptions are subclasses
All exceptions are subclasses of a class called
java.lang.Throwable
How do you intercept and thereby control exceptions
We can do this by using try/catch/finally blocks
You place the normal processing code in try block
You put the code to deal with exceptions that might arise in try block in catch
block
Code that must be executed no matter what happens must be place in finally
block
When do we say an exception is handled
When an exception is thrown in a try block and
is caught by a matching catch block, the exception is considered to have been
handled
When do we say an exception is not handled
There is no catch block that names either the
class of exception that has been thrown or a class of exception that is a
parent class of the one that has been thrown, then the exception is considered
to be unhandled, in such condition the execution leaves the method directly as
if no try has been executed
In what sequence does the finally block gets executed
If you put finally after a try block without a
matching catch block then it will be executed after the try block
If it is placed after the catch block and there is no exception then also it
will be executed after the try block
If there is an exception and it is handled by the catch block then it will be
executed after the catch block
What can prevent the execution of the code in finally block
- The death of thread
- Use of system.exit()
- Turning off the power to CPU
- An exception arising in the finally block itself
What are the rules for catching multiple exceptions
A more specific catch block must precede a more general
one in the source, else it gives compilation error
Only one catch block, that is first applicable one, will be executed
What does throws statement declaration in a method indicate
This indicates that the method throws some
exception and the caller method should take care of handling it
What are checked exception
Checked exceptions are exceptions that arise in
a correct program, typically due to user mistakes like entering wrong data or
I/O problems
What are runtime exceptions
Runtime exceptions are due to programming bugs
like out of bond arrays or null pointer exceptions.
What is difference between Exception and errors
Errors are usually compile time and exceptions
can be runtime or checked
How will you handle the checked exceptions
You can provide a try/catch block to handle it.
OR
Make sure method declaration includes a throws clause that informs the calling
method an exception might be thrown from this particular method
When you extend a class and override a method, can this new method throw exceptions
other than those that were declared by the original method
No it cannot throw, except for the subclasses of those exceptions
Is it legal for the extending class which overrides a method which
throws an exception, not o throw in the overridden class
Yes it is perfectly legal
Explain the user defined Exceptions?
User defined Exceptions are the separate
Exception classes defined by the user for specific purposed. An user defined
can created by simply sub-classing it to the Exception class. This allows custom
exceptions to be generated (using throw) and caught in the same way as normal
exceptions.
Example:
class myCustomException extends Exception {
// The class simply has to exist to be an exception
}
No comments:
Post a Comment