Java Exception Handling

Java Exception handling is a framework that is used to handle runtime errors only, compile time errors are not handled by exception handling in java.

The exceptions have java.lang.Throwable as their superclass and it has two child objects – Error and Exception. Exceptions are further divided into checked exceptions and runtime exception.The three main categories of Exceptional conditions are :

  • Error(represented by java.lang.Error and its sub-classes)
  • Checked Exception(represented by direct subclasses of java.lang.Exception except java.lang.RuntimeException)
  • Unchecked or Runtime Exceptions (represented by java.lang.RuntimeException and its sub-classes)

Errors : Errors denote serious abnormal condition with the application like OutOfMemoryError or VirtualMachineError. Any reasonable application should not try to recover from such a condition.

Runtime Exception/Unchecked Exception : These types of exceptions usually indicate programming errors like NullPointerException or IllegalArgumentException. The application may or may not choose to recover from the condition.

Checked Exception : An application is supposed to catch these types of exceptions and recover reasonably from them. Examples include FileNotFoundException and ParseException.

ExceptionHierarchy

Some of the useful methods of Throwable class are:
public String getMessage()
public String toString()
public void printStackTrace()

Java Exception Handling Keywords:
     throw, throws, try-catch, finally

finally – finally block is optional and can be used only with try-catch block. Since exception halts the process of execution, we might have some resources open that will not get closed, so we can use finally block. finally block gets executed always, whether exception occurred or not.

Custom exceptions can be created by extending the Exception class.

Questions:

1) What is Exception in Java?

  • Exception is an error event that can happen during the execution of a program and disrupts it’s normal flow. Exception can arise from different kind of situations such as wrong data entered by user, hardware failure, network connection failure etc.

    Whenever any error occurs while executing a java statement, an exception object is created and then JRE tries to find exception handler to handle the 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 terminates the program.

    Java Exception handling framework is used to handle runtime errors only, compile time errors are not handled by exception handling framework.

 

2) What is OutOfMemoryError in Java?

OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError and it’s thrown by JVM when it ran out of heap memory. We can fix this error by providing more memory to run the java application through java options.

$>java MyProgram -Xms1024m -Xmx1024m -XX:PermSize=64M -XX:MaxPermSize=256m

 

3) What happens when exception is thrown by main method?

When exception is thrown by main() method, Java Runtime terminates the program and print the exception message and stack trace in system console.

 

4) What is difference between final, finally and finalize in Java?

final and finally are keywords in java whereas finalize is a method.

final keyword can be used with class variables so that they can’t be reassigned, with class to avoid extending by classes and with methods to avoid overriding by subclasses, finally keyword is used with try-catch block to provide statements that will always gets executed even if some exception arises, usually finally is used to close resources. finalize() method is executed by Garbage Collector before the object is destroyed, it’s great way to make sure all the global resources are closed.

Out of the three, only finally is related to java exception handling.

5) What is difference between Checked and Unchecked Exception in Java?

Checked Exceptions should be handled in the code using try-catch block or else main() method should use throws keyword to let JRE know about these exception that might be thrown from the program. Unchecked Exceptions are not required to be handled in the program or to mention them in throws clause.

Exception is the super class of all checked exceptions whereas RuntimeException is the super class of all unchecked exceptions.

Checked exceptions are error scenarios that are not caused by program, for example FileNotFoundException in reading a file that is not present, whereas Unchecked exceptions are mostly caused by poor programming, for example NullPointerException when invoking a method on an object reference without making sure that it’s not null.

 

Links: https://examples.javacodegeeks.com/java-basics/exceptions/java-custom-exception-example/, http://www.journaldev.com/1696/exception-handling-in-java

Questions on Exceptions: http://www.journaldev.com/2167/java-exception-interview-questions-and-answers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s