- What is an exception?
The English literature meaning is "something that happens unusually (very rarely)". It is the same meaning with Java also. Exception can be defined as a problem occurring at runtime that prevents the execution of the program. Or to say, an exception is a runtime problem (not compile time as compilation is already over) that terminates the program execution abruptly.
- What are the causes of exceptions to occur?
It is simply due to the end-user (actual user of the software). It is due to the wrong interaction with a running system feeding wrong data which the program execution does not understand what to do or cannot give any output depending on the data user feeds. For example, entering zero for a denominator value or entering a wrong IP address in a network to connect etc.
- What is exception handling or why should we handle the exception?
When an exception is thrown by the JVM, the execution simply terminates without executing the expected whole program. To continue the execution, even if an exception occurs, exception handling is required. Handling the exception thrown the by JVM so that the program’s execution goes further is known as "exception handling".
- What are try and catch blocks?
"try" and "catch" are keywords of Java used with exception handling mechanism only. When the code is being developed, the programmer should place the trouble making statements (generally the statements that ask the user to enter some data) in a try block with a suitable exception handler in catch block.
- What is an exception handler?
Exception handler is a Java class capable to handle the exception thrown by the system (JVM). Exception handler is placed in the catch statement. It is very important to place the correct handler that can catch the exception successfully. When an exception is thrown by the try block, the catch block catches the exception and if the try block does not throw any exception (due to correct feed of data), the catch block execution is simply ignored (that is, does not execute at all like else will not be executed when if is executed).
- What is finally block?
“finally” is a keyword of Java used with exception handling only. If the programmer places a wrong handler with catch statement, the exception handling mechanism does not work and thereby the execution terminates without executing all the remaining statements. In the remaining statements, a few may be very important that must be executed before the program gets terminated. Place these important statements in "finally" block. That is, finally block statements are guaranteed of execution even if the programmer does not handle the exception successfully. Having finally block is optional. Generally, finally block comes with combination of try-catch.
- Mention some important statements that require fianlly block attention?
The statements include like closing statements of IO stream and socket hanldes, database connections etc. (known as cleanup operations that prevents memory leakage).
- What is throws keyword?
"throws" keyword can be used in two ways in Java.
- Used to inform the programmer about the possible problems that may occur while using the method known as "claiming the exception".
- As an alternative to try-catch block.
- What is throw keyword?
Using throw keyword, the programmer can throw the exception to the system (JVM) explicitly.
- What is the difference between "throws" and "throw"?
Both look alike but they differ a lot in their usage. throws is used to claim the exception and throw is used to throw the exception explicitly.
- How many ways an exception can be handled in Java?
There are three ways to handle.
- With try-catch block.
- With throws.
- With throw.
- Can a catch exist without try or finally exist without catch?
catch should come in combination with try block like else cannot exist wihout if. catch block can be replaced with finally block. That is, with try there can come either catch or finally. They best combination, generally used is, try-catch-finally.
- What is the super class of all exceptions of Java?
It is class Throwable from java.lang package.
- What is Error exception in Java?
It is an unchecked exception and a subclass of Throwable. It is generally used for handling the exceptions thrown by the JVM at runtime like memory insufficient etc.
- What is the rule to be observed regarding exceptions in method overriding?
The overridden method in subclass cannot throw more exceptions than that of super class method. There is a rule to be followed with access specifiers also in method overriding.
- What is NumberFormatException?
It is thrown by the parsing methods when the method is unable to parse (convert) the string data into primitive data types.
Pass your comments on this tutorial "Exceptions Interview Questions" to improve the quality.