You are on page 1of 4

What is Exception?

-Exception is an error that can happen during the execution of program and can break its normal flow.
-Whenever exception occurs while executing a program, an exception object is created and then JRE tries to call exception handler to
handle exception.
-If suitable exception handler is found then the exception object is passed to the handler code to process the exception, known as
catching the exception.
-If no handler is found, then application throws the exception to runtime environment and JRE terminate the program.
-Compile time errors are not handled by exception handling framework. Only Run time error can only be handled by Exception
Handling framework.

Compile time exception – syntax error.


Run time exception – wrong input.

What are the exception handling Key-word?

Throws: when we are throwing any exception in a method and not handling it, then we need to use throws keyword in method
signature to let the caller program know the exception that might be thrown by the method.
We can provide multiple exceptions in the throws clause.

Throw: Sometime we want to create an exception object and then throw it to halt the normal processing of the program.Then used
throw keyword .when no record found then throw RecordNotFound Exception.
if(list==null)
{
throw new RecordNotFoundException();
}
Try-Catch: try block contain the code that might cause the exception and catch block can handle the exception created by try block.
Single try block may contain multiple catch exception.

Finally:
-finally, block is optional and can be used only with try catch block.
-finally block will always execute whether exception occurs or not.
-finally block can be useful for resource handling.
Java Exception Hierarchy?
Throwable - is the parent class of java exceptions and it has two child object Error and Exception.
Errors: scenario that are out of scope of application and it’s not possible to recover from them.
Hardware failure and Out of Memory exception.

Exceptions are further divided into checked exception and runtime exception.
Checked Exception: FileNotFoundException we should catch this exception and can provide use full message to the user.
Runtime Exception: caused by bad programming.
ArrayIndexOutOfBoundException 
NullPointerException.
ArithmaticException.

Java 7 ARM Feature? OR Try-with-resource


Finally block used to close the resource explicitly and sometime we forget to close them and get exception. These exceptions are hard
to debug and we need to look each every place to make sure we are closing the resource.
Try-with-resource where we can create a resource in try statement itself. When the execution comes out of try-catch block,runtime
environment automatically close these resource .

How to write custom exception in java?


We can extend Exception class or any of its subclass to create our custom exception class. The custom exception class can have its
own variable and methods.

What is OutOfMemory in Java ?


Thrown by JVM when it ran out of heap memory. We can fix this error by providing more memory to run the java application.

Provide some java exception handling best practices?


-Used specific exception for ease of debugging.
-Used java 7 ARM feature to make sure resources are closed or used finally block to close them properly.
-Always log exception message for debugging purpose.
-Used custom exception to throw single type of exception from your application API.
-Exception are costly so throw it only it makes sense else you can catch and provide null or empty response.

Error Exceptions
1 Error are of type ‘Unchecked’. Exception can be either ‘checked’ or ‘unchecked’.
2 Occur at runtime Occur at compile time and runtime
3 Caused by the application running environment. Caused by the application itself.
4 Impossible to recover from error. It is possible to recover from exception using try-catch block.
5
StackOverFlowError: If JVM encounters a situation where there is no space for a new stack frame to be
created, it will throw a StackOverflowError.

OutOfMemoryError: This error is thrown when there is insufficient space to allocate an object in


the Java heap.

You might also like