Question 1. What Is Exception Handling?
Exception Handling in Java offers a way to deal with a state of affairs when an exception is thrown and shows a significant message to the person and continue with the waft of this system.
When an splendid condition takes place with in a technique, the technique (wherein the exception befell) creates an Exception Object and throws it. The created exception item incorporates data approximately the error, its type and the nation of this system whilst the mistake came about.
The technique where the exception is thrown may also handle that exception itself or bypass it on. In case it passes it on, run time device goes through the technique hierarchy that had been called to get to the modern approach to look for a method which can handle the exception.
Five keywords used to manage Java exception managing
strive - Any code that would throw an exception is enclosed within a strive block.
Seize - If an exception occurs in try block, trap block can provide exception handlers to handle it in a rational manner.
Finally - The subsequently block usually executes whilst the strive block exits. So, any code that ought to execute after a attempt block is completed should be put in in the end block.
Throw - throw is used to manually thrown an exception.
Throws - Any exception that is thrown in a way but now not handled there need to be specified in a throws clause.
Question 2. Explain The Exception Hierarchy In Java?
Throwable elegance is the wonderful elegance of all the exception sorts. Below Throwable magnificence there are subclasses which denotes distinct branches of exceptions -
Exception - An Exception shows that a hassle has came about, however it isn't always a severe gadget problem. The person applications you write will throw and seize Exceptions.
Error - It defines exceptions that are not predicted to be stuck via your program. Exceptions of type Error are utilized by the Java run-time gadget to signify errors having to do with the run-time environment, itself.
Examples of error are StackOverflowError, OutOfMemoryError etc.
Below Exception there may be a wonderful subclass RunTimeExcpetion - RunTimeExcpetion and its descendants denote the exceptional situations which might be external to the software, and the application normally can't anticipate or get over them.
Adv Java Interview Questions
Question 3. What Is The Difference Between Checked Exception And Unchecked Exception?
Checked Exception is a direct subclass of Exception where as unchecked exception is a subclass of RunTimeException.
Checked exception must be wrapped in a try-capture block or detailed as throws clause wherein as there may be no such requirement for unchecked exception.
Failure to provide exception coping with mechanism for checked exception bring about compiler blunders whereas no compile time mistakes for unchecked exception.
Checked exceptions are designed to reduce the wide variety of exceptions which aren't well handled and where there is an inexpensive threat for healing. UnCheckedExceptions are mainly programming mistakes.
Question 4. What Is The Difference Between Error And Exception?
Exception - An Exception suggests that a trouble has passed off, however it is not a extreme device problem. The person programs you write will throw and catch Exceptions.
Error - It defines exceptions that are not anticipated to be stuck by your application. Exceptions of kind Error are utilized by the Java run-time machine to signify mistakes having to do with the run-time surroundings, itself.
Examples of mistakes are StackOverflowError, OutOfMemoryError and many others.
Adv Java Tutorial
Question 5. Is It Necessary That Each Try Block Must Be Followed By A Catch Block?
No it is not mandatory that there need to be a capture block after a try block. Try block can have only an identical ultimately block. So there are those valid combnations attempt-catch-finally, strive-seize, try-eventually.
J2EE Interview Questions
Question 6. What Is Finally Block?
When an exception takes place within the code, the drift of the execution may additionally alternate or even give up all of sudden. That may additionally cause problem if a few assets have been opened within the technique.
As exp if a file changed into opened in a method and it changed into not closed in the long run as a few exception happened then the assets may additionally continue to be open consuming memory. Ultimately provides that exception-dealing with mechanism to smooth up.
Code with inside the ultimately block can be completed after a attempt/trap block has completed. The in the end block could be performed whether or not or not an exception is thrown.
Question 7. Is It Possible To Have A Finally Block Without Catch?
Yes we are able to have a strive-finally block, trap is optional. We may have these mixtures attempt-seize-eventually, attempt-catch, strive-eventually.
J2EE Tutorial Core Java Interview Questions
Question 8. What Is A Nested Try Statement?
A strive-catch-eventually block can reside internal any other strive-trap-in the end block that is called nested attempt declaration.
Public elegance NestedTryDemo
public static void essential(String args)
System.Out.Println("In Outer attempt block");
System.Out.Println("In Inner strive block");
int a = 7 / 0;
catch (IllegalArgumentException e)
System.Out.Println("In Inner in the end");
capture (ArithmeticException e)
in the end
System.Out.Println("In Outer finally");
Question 9. What Are Multiple Catch Blocks?
There might be a case whilst a code enclosed with in a try block throws a couple of exception. To cope with those kinds of conditions, or more seize clauses can be precise in which each seize clause catches a distinctive kind of exception. When an exception is thrown, each of the capture statement is inspected so as, and the primary one whose kind suits that of the thrown exception is finished.
Int a = zero;
int b = 7/a[i];
JSP Interview Questions
Question 10. What Is Exception Propagation?
When an top notch condition occurs inside a method, the method (in which the exception befell) creates an Exception Object and throws it. The created exception object incorporates facts approximately the error, its type and the country of this system when the mistake came about.
The approach in which the exception is thrown may additionally handle that exception itself or skip it on. In case it passes it on, run time device goes via the technique hierarchy that had been referred to as to get to the modern-day approach to look for a method that may deal with the exception.
If your application isn't able to capture any specific exception, in order to in the end be processed by the default handler. This technique of going thru the approach stack is known as Exception propagation.
Core Java Tutorial
Question eleven. What Is Throw Keyword?
It is feasible for a Java application to throw an exception explicitly this is finished the usage of the throw assertion.
The widespread shape of throw is -
We can get this throwableObject in 2 approaches -
By the usage of the Exception parameter of catch block.
Create a new one the use of the brand new operator.
throw new NullPointerException();
System.Out.Println("Exception caught in seize block of displayValue");
Java-Springs Interview Questions
Question 12. What Is Throws Clause?
If in a method we do not want to deal with any exception however need to depart it to the calling method to handle any exception this is thrown by means of the known as approach, it is carried out using throws key-word.
Using throws a way can just claim the exception it can throw and callers of the method should offer exception dealing with for the ones exceptions (or they can also declare them the usage of throws).
General form of a way announcement that consists of a throws clause
type technique-call(parameter-listing) throws exception-listing
// body of technique
Adv Java Interview Questions
Question thirteen. Difference Between Throw And Throws?
throw is used to throw an exception.
Throws is used to declare an exception, in the approach signature, that can be thrown from a technique.
Question 14. Final Vs Finally Vs Finalize?
final - final keyword is used to restrict in a few way. It can be used with variables, techniques and instructions. When a variable is said as final, its cost cannot be changed once it's miles initialized. Except in case of clean final variable, which ought to be initialized within the constructor.
If you're making a technique very last in Java, that technique cannot be overridden in a sub elegance.
If a class is asserted as very last then it can not be sub classed.
Eventually - sooner or later is part of exception handling mechanism in Java. Ultimately block is used with strive-catch block. In the end block is constantly carried out whether or not any exception is thrown or no longer and raised exception is handled in trap block or no longer. Since subsequently block usually executes accordingly it is by and large used to close the opened assets like database connection, document handles and many others.
Finalize() - finalize() technique is a protected method of java.Lang.Object class. Since it is in Object magnificence thus it is inherited by means of every magnificence. This technique is referred to as by way of rubbish collector thread earlier than disposing of an object from the reminiscence. This method can be overridden with the aid of a class to offer any cleanup operation and offers item very last hazard to cleanup before getting garbage accumulated.
Included void finalize() throws Throwable
//aid smooth up operations
Question 15. What Are The Rules Of Exception Handling With Respect To Method Overriding?
There are positive regulations at the same time as overriding a technique in case of exception coping with in Java. Broadly there are two guidelines -
If superclass method has no longer declared any exception using throws clause then subclass overridden method cannot declare any checked exception though it is able to declare unchecked exception.
If superclass technique has declared an exception using throws clause then subclass overridden approach can do one of the three things.
Sub-elegance can declare the identical exception as declared within the extraordinary-elegance approach.
Subclass can declare the subtype exception of the exception declared within the superclass method. But subclass technique cannot declare any exception that is up inside the hierarchy than the exception declared in the extraordinary class approach.
Subclass approach can select no longer to declare any exception in any respect.
JMS(Java Message Service) Interview Questions
Question sixteen. What Is The Error In The Following Code?
18. Class Parent
19. Public void displayMsg() throws IOException
20. System.Out.Println("In Parent displayMsg()");
21. Throw new IOException("Problem in approach - displayMsg - Parent");
24. Public elegance ExceptionOverrideDemo extends Parent
25. Public void displayMsg() throws Exception
26. System.Out.Println("In ExceptionOverrideDemo displayMsg()");
27. Throw new Exception("Problem in technique - displayMsg - ExceptionOverrideDemo");
Here determine class had declared IOException in which as subclass has declared Exception. Exception is the awesome elegance of IOException therefore it is inaccurate according to the guidelines of method overriding and exception coping with. Thus the code will give compiler mistakes.
Question 17. What Is Multi-trap Statement In Java 7?
Before Java 7 multi-catch assertion, if or greater exceptions have been treated in the same manner, we nevertheless had to write separate capture blocks for coping with them.
With Java 7 and later it is feasible to trap more than one exceptions in one capture block, which eliminates the duplicated code. Each exception type within the multi-seize statement is separated with the aid of Pipe symbol (trap SQLException exp)
Java applet Interview Questions
Question 18. What Is Try-with-resources Or Arm In Java 7?
Java 7 delivered a brand new form of strive known as strive-with-assets for Automatic Resource Management (ARM). Here aid is an object that need to be closed after the program is completed with it. Example of sources might be an opened file take care of or database connection etc.
Before the advent of try-with-assets we needed to explicitly close the sources as soon as the strive block completes generally or all at once.
br = new BufferedReader(new FileReader("C:take a look at.Txt"));
catch (IOException e)
if (br != null)
System.Out.Println("Closing the file");
trap (IOException ex)
try-with-resources facilitates in reducing such boiler plate code. Let's see the identical example the usage of strive-with-resources.
Try(BufferedReader br = new BufferedReader(new FileReader("C:test.Txt")))
seize (IOException e)
J2EE Interview Questions
Question 19. When Is Custom Exception Class Needed? How To Create A Custom Exception Class?
According to Java Docs, you should write your very own exception lessons if you answer sure to any of the following questions; in any other case, you could likely use someone else's.
Do you want an exception type that isn't always represented through the ones within the Java platform?
Would it assist customers if they could differentiate your exceptions from those thrown by using training written by means of other carriers?
Does your code throw a couple of related exception?
F you use a person else's exceptions, will customers have get right of entry to to the ones exceptions? A similar query is, must your bundle be unbiased and self-contained?