Exceptions

An exception in Java is an unexpected or unwanted event. It usually occurs when a program is executed i.e. at runtime. An exception interferes with the normal flow of a program’s instructions.

Is there a difference between error and exception?

The main difference is that exceptions can be handled at runtime, while errors cannot be handled. Errors cause you to exit from your current program. Additionally, all errors are exceptions. However, the reverse is not true

Exception Hierarchy

Our Java programming homework help experts say that all errors and exception types are subclasses of class “Throwable.” This class is a base class of hierarchy. The sub-class exception is used for exceptional conditions that user programs should catch. An example of such exceptional conditions is the NullPointerException. The other sub-branch, error, is used by the JVM (Java Run-time Machine) to identify errors dealing with the run-time environment (JRE). An example of JRE errors is StackFlowError.

We hope that the diagram below can help you understand this hierarchy better:

For help with checked and unchecked exceptions, get in touch with our experts via our live chat facility.

How the Java Virtual Machine handles an exception

  • Default exception handling

If an exception occurs inside a method, an exception object is created and handed over to the Java Run-time System. The exception object usually has vital information regarding the name and description of the exception and the program where the exception has occurred. This process of creating an exception object and handing it over to the JVM is called throwing an exception. Moreover, there is also a list of methods that is often called when the system wants to know the exact method where the exception has occurred. This ordered list is known as a Call Stack.

After calling the Call Stack, the following process will happen:

  • The JVM system searches the call stack to locate the method with the block of code that can handle the exception that has occurred. This block of code is also known as the Exception Handler.
  • Next, the system searches for the method in which the exception occurred. It goes through the call stack in a reverse order in which the methods were called.
  • The JVM then passes the occurred exception to an appropriate handler. An appropriate exception handler is one that can handle the exception object thrown to it.
  • The run-time system will hand over the exception object to the default exception handler if it searches all the methods in the call stack and cannot find an appropriate handler. The default exception handler is part of the run-time system. It terminates the program abnormally and prints information on the exception in the following format:

Exception in thread “XXX” Exception Name: Description

. . .   . . . . . .   . .  // Call stack

The diagram below will help you understand the flow of the call stack

The Java code sample below shows an exception is thrown:

//Java program demonstrating exception throwing

Class ThrowsExcep {

Public statistic Void main (String args []) {

String str = null;

System.out.printIn( str.length());

}

}

When you run this code, the output will be:

Exception in thread “main” java.lang.NullPointerException

At ThrowsExcep.main (File.java:8)

How a programmer can handle an exception

  • Customized handling of exception

Five keywords are used to manage Java exception handling. They are try, throw, catch, throws, and finally. Here is a brief description of how these keywords work. A try block contains all the program statements that can raise an exception. In case an exception occurs in the try block, it is thrown. Your code will try to use the catch block to catch this exception and handle it rationally.

Exceptions that are generated by the system are automatically thrown to the JVM. If you want to throw an exception manually, you should use the keyword throw. A throws clause is used to specify any exception thrown out of a method as such. The finally block has all the codes that must be executed after a try block completes.

Critical points that you should remember

  • A method can have more than one statements that might throw an exception A programmer should put all these statements within a try block and provide separate exception handler within their own catch block.
  • Exceptions that occur within the try block are handled by the exception handler associated with them.
  • In every try block, there can be zero or more catch block. However, every try block has only one finally block
  • A finally block always gets executed regardless if an exception occurs in a try block or not. This means that it is optional. The finally block is executed after the try and catch blocks if an exception occurs. On the other hand, it will be executed after the try block if an exception does not happen. In Java, the finally block has important codes like the clean-up code.

Avail help with java programming assignment help right here and be guaranteed of excellent grades.