top

Search

Java Tutorial

An exception is an unwanted or unexpected event that occurs during program execution, i.e. at runtime, disrupting the normal flow of instructions from the program. An Error indicates a serious problem that a reasonable application should not try to catch. It’s an issue and needs to be fixed.An exception is a condition that a reasonable application might try to catch or handle. Below is a class hierarchy of exception classes:  All types of exceptions and errors are subclasses of the Throwable class, which is the hierarchy base class. Exception leads to one branch. This class is used for exceptional conditions that user programs should catch. Example: NullPointerException. Another branch, the Java run-time system(JVM) uses Error to show mistakes related to the run-time setting (JRE) itself. Example: StackOverflowError Default Exception Handling by Java: Whenever an exception has occurred inside a method, the method creates an object known as an exception object and transfers it to the runtime system(JVM). The exception object includes the exception name and description, and the present program states where an exception happened. It is called throwing an exception to create the exception object and handle it to the run-time system. There might be a list of methods called to get to the method where there was an exception. Call Stack is called this ordered list of methods. Now the following procedure will happen. The runtime system is searching the call stack to locate the method that includes code block that can manage the exception that has happened. The code block is called the Exception handler. The runtime system begins to search the method in which an exception happened, in the inverse order in which methods were called If it discovers a suitable handler, then it moves the exception that has happened to it. Appropriate handler implies the sort of the thrown exception object matches the sort of exception object it is capable of handling. If the runtime system searches all methods on the call stack and the appropriate handler could not have been found, then runtime system handover to the default exception handler, which is part of a runtime system. This handler prints in the following format the exception data and abnormally terminates the program. Exception in thread "xxx" Name of Exception: Description  ... ...... ..  // All call Stack  See the diagram below to comprehend the call stack's flow. Customized Exception Handling in java: Five keywords are used to manage Java exception handling: try, catch, throw, throws and finally. Briefly, the way they operate is here. Statements of the program that you believe may raise exceptions are contained in a try block. If there is an exception within the try block, it will be thrown. Your software can capture and manage this exception in some rational way (using the catch block). The Java runtime system automatically throws system-generated exceptions. Use the keyword throw to manually insert an exception. Any exception that is thrown from a method must be defined by a throws clause as such. Any code that must be performed after completing a try block is placed in a final block. Below is an example as how to use try-catch clause: try {  // the code you think can throw an exception  }  catch (Exception1 ex1) {  // exception handler for Exception1  }  catch (Exception2 ex2) {  // exception handler for Exception2  }  finally {  // block of code to be executed after try block ends whether exception occurred or not.  } Points to remember in exception handling in java: In a method, there may be more than one statement that could throw an exception, so bring all these statements into their own try block and provide distinct exception handlers for each of them within their own catch block. If an exception occurs within the try block, the exception handler associated with it will handle that exception. We need to bring the catch block after it to be associated with the exception handler. There may be more than one handler of exceptions. Each catch block is an exception handler that handles its argument's type exception. The argument, Exception Type, says the exception type that it can manage and must be the class name that inherits from Throwable class. There may be zero or more catch blocks for each try block, but at last only one finally block. The final block is optional at last. Whether an exception existed in a try block or not, it is always performed. If an exception happens, after attempting to catch blocks, it will be performed. And if there is no exception then after the try block it will be performed. Ultimately, this block in java is used to put significant codes such as cleaning up code, e.g. file closure or link closure. Summary: 
logo

Java Tutorial

Exceptional Handling

An exception is an unwanted or unexpected event that occurs during program execution, i.e. at runtime, disrupting the normal flow of instructions from the program. 

An Error indicates a serious problem that a reasonable application should not try to catch. It’s an issue and needs to be fixed.An exception is a condition that a reasonable application might try to catch or handle. 

Below is a class hierarchy of exception classes:  

Hierarchy of exception classes

All types of exceptions and errors are subclasses of the Throwable class, which is the hierarchy base class. Exception leads to one branch. This class is used for exceptional conditions that user programs should catch. Example: NullPointerException. 

Another branch, the Java run-time system(JVM) uses Error to show mistakes related to the run-time setting (JRE) itself. Example: StackOverflowError

Default Exception Handling by Java: Whenever an exception has occurred inside a method, the method creates an object known as an exception object and transfers it to the runtime system(JVM). The exception object includes the exception name and description, and the present program states where an exception happened. It is called throwing an exception to create the exception object and handle it to the run-time system. There might be a list of methods called to get to the method where there was an exception. Call Stack is called this ordered list of methods. 

Now the following procedure will happen. 

  • The runtime system is searching the call stack to locate the method that includes code block that can manage the exception that has happened. The code block is called the Exception handler. 
  • The runtime system begins to search the method in which an exception happened, in the inverse order in which methods were called 
  • If it discovers a suitable handler, then it moves the exception that has happened to it. Appropriate handler implies the sort of the thrown exception object matches the sort of exception object it is capable of handling. 
  • If the runtime system searches all methods on the call stack and the appropriate handler could not have been found, then runtime system handover to the default exception handler, which is part of a runtime system. This handler prints in the following format the exception data and abnormally terminates the program. 
  • Exception in thread "xxx" Name of Exception: Description 
  •  ... ...... ..  // All call Stack  

See the diagram below to comprehend the call stack's flow. 

call stack's flow

Customized Exception Handling in java: Five keywords are used to manage Java exception handling: try, catch, throw, throws and finally. Briefly, the way they operate is here. Statements of the program that you believe may raise exceptions are contained in a try block. If there is an exception within the try block, it will be thrown. Your software can capture and manage this exception in some rational way (using the catch block). The Java runtime system automatically throws system-generated exceptions. Use the keyword throw to manually insert an exception. Any exception that is thrown from a method must be defined by a throws clause as such. Any code that must be performed after completing a try block is placed in a final block. 

Below is an example as how to use try-catch clause: 

try { 
// the code you think can throw an exception 
} 
catch (Exception1 ex1) { 
// exception handler for Exception1 
} 
catch (Exception2 ex2) { 
// exception handler for Exception2 
} 
finally { 
// block of code to be executed after try block ends whether exception occurred or not. 
} 

Points to remember in exception handling in java: 

  • In a method, there may be more than one statement that could throw an exception, so bring all these statements into their own try block and provide distinct exception handlers for each of them within their own catch block. 
  • If an exception occurs within the try block, the exception handler associated with it will handle that exception. We need to bring the catch block after it to be associated with the exception handler. There may be more than one handler of exceptions. Each catch block is an exception handler that handles its argument's type exception. The argument, Exception Type, says the exception type that it can manage and must be the class name that inherits from Throwable class. 
  • There may be zero or more catch blocks for each try block, but at last only one finally block. 
  • The final block is optional at last. Whether an exception existed in a try block or not, it is always performed. If an exception happens, after attempting to catch blocks, it will be performed. And if there is no exception then after the try block it will be performed. Ultimately, this block in java is used to put significant codes such as cleaning up code, e.g. file closure or link closure. 

Summary: 

Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

protective orders in virginia

Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.

Johnfrance

Thank you for your valuable information.

sam

Thank you for this wonderful article!

Belay Abera

This article was really helpful to me, thank you!

dfsdf

super article!