31) What is the difference between final, finally and finalize() ?
final is a modifier which can be applied to a class or a method or a variable. final class can’t be inherited, final method can’t be overridden and final variable can’t be changed.
Java finally block is a block that is used to execute important code such as closing connection, stream etc. Java finally block is always executed whether exception is handled or not. Java finally block follows try or catch block.
finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.
32) What is exception chaining in Java ?
The chained exception feature allows you to associate another exception with an exception. This second exception describes the cause of the first exception. For example, imagine a situation in which a method throws an ArithmeticException because of an attempt to divide by zero. However, the actual cause of the problem was that an I/O error occurred, which caused the divisor to be set improperly. Although the method must certainly throw an ArithmeticException, since that is the error that occurred, you might also want to let the calling code know that the underlying cause was an I/O error. Chained exceptions let you
33) What is Exception Propagation in Java ?
When an exception is thrown, the runtime system searches the call stack for a method that contains a block of code that can handle the exception. The search begins with the method in which the error occurred and proceeds through the call stack in the reverse order in which the methods were called. When an appropriate handler is found, the runtime system passes the exception to the handler. This is called as exception propagation.
For ex. Let’s say we have 3 methods, method1(), method2() and method3() where method1() is calling method2() and in turn method2() is calling method3().
If an exception occurs in method3() and if method3() don’t have any exception handler, then the uncaughtht exception will be propagated downward in stack i.e it will check for appropriate exception handler in the method2(). If method2() also don’t have any exception handler then again exception is propagated downward to method1() where it finds exception handler.
34) What is StackOverflowError in Java ?
The StackOverFlowError is an Error Object thrown by the Runtime System when it encounters that if there is no memory left in stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError.
35) What is OutOfMemoryError in java ?
OutOfMemoryError is the sub class of java.lang.Error which occurs when JVM runs out of memory. OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError
36) How to create custom exception in Java ?
To create a custom exception in Java, extend Exception class or any of it’s subclasses. The custom exception class can have it’s own data members and methods that we can use to pass error codes or other exception related information to the exception handler.
37) What is Stack Trace in Java ?
Stack Trace is a list of method calls from the point when the application was started to the point where the exception was thrown. The most recent method calls are at the top. The stack trace can be printed to the console by calling the public void printStackTrace() method of an exception.
38) What is the use of printStackTrace() method ?
printStackTrace() method is used to print the detailed information about the exception occurred.
39) What are the Java exception handling best practices ?
Some of the exception handling best practices are:
- If you know that the method might throw an exception, then declare the list of exceptions in method signature using throws clause or handle the exceptions using try-catch-finally block.
- Log exception messages for debugging purposes.
- Do not try to catch the error.
- Throw exceptions early in the program and catch exceptions late in the program.
- Never catch Throwable class
- Do not use printStackTrace() for printing the exception log, rather use logging framework to log exceptions.