Home » Java programming language

Basics of Exception Handling In Java

Learn: What is Exception? How to handle exceptions in java program and basics of some related terms and concepts.
Submitted by Abhishek Jain, on September 02, 2017

Exception - that means exceptional errors. Actually, exceptions are used for handling errors in programs that occurs during the program execution.

You can implement exception-handling in your program by using the following keywords:

  1. try - This block catches series of errors at JRE and throw it to the catch block.
  2. catch - Catches the error thrown by the try{} block.
  3. throw - Throw Keyword is used to throw an exception explicitly or manually.
  4. throws- It is use to ignore try and catch block within the function.
  5. finally - This block execute at last whether an error occurred or not.

If any error occurs during the program execution and you want to print your own message or the system message about the error thenyou can write the part of the program within the try{} block and catches the errors using catch() block.

  • Exception turns the direction of normal flow of the program control and send to the related catch() block.
  • Error that encounters during the execution produces an instance (object) which holds the information or message regarding the errors occurred in the program.
  • Errors can be broadly categorized into two groups on the basis of whether the compileris able to handle the error or not, such as compile time errors and runtime errors.
  • An exception is a run-time error that can be defined as an abnormal event that occursduring the execution of a program and disrupts the normal flow of instructions.
  • The built-in exceptions in Java are divided into two types on the basis of the conditionswhere the exception is raised:
    • Checked Exceptions or Compiler-enforced Exceptions
    • Unchecked exceptions or Runtime Exceptions
  • You use multiple catch blocks to throw more than one type of exception.
  • You can create your own exception classes to handle the situations specific to anapplication.

Java's Built-in Exceptions

Inside the standard package java.lang, Java defines several exception classes.

Example: ArithmeticException ,ArrayIndexOutofBoundsException ,IOException ,ClassNotFoundException , etc.

The most general of these exceptions are subclasses of the standard type RuntimeException. In the language of Java, these are called uncheckedexceptionsbecause the compiler does not check to see if a method handles or throws these exceptions.Those exceptions defined by java.langthat must be included in a method’s throwslist if that method can generate one of these exceptions and does not handle it itself. These are called checked exceptions.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.