Nov 29 2009

Java Tutorial : Exception-Handling – Summary

Category: Javaalexi @ 12:11 pm
  • Errors can be broadly categorized into two groups on the basis of whether the compiler is 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 occurs during the execution of a program and disrupts the normal flow of instructions.
  • In Java, the Throwable class is the superclassof all the exception classes. It is the class at the top position in the exception class hierarchy. The Exception class and the Error class are two direct subclasses of the Throwable class.
  • The built-in exceptions in Java are divided into two types on the basis of the conditions where the exception is raised:
    • Checked Exceptions or Compiler-enforced Exceptions
    • Unchecked exceptions or Runtime Exceptions
  • You can implement exception-handling in your program by using the following keywords:
    • try
    • catch
    • throw
    • throws
    • finally
  • You use multiple catch blocks to throw more than one type of exception.
  • The finally clause is used to execute the statements that need to be executed whether or not an exception has been thrown.
  • The throw statement causes termination of the normal flow of control of the
  • Java code and stops the execution of subsequent statements after the throw statement.
  • The throws clause is used by a method to specify the types of exceptions the method throws.
  • You can create your own exception classes to handle the situations specific to an application.
  • You can use assertions to check and ensure that any assumption made at the start of a program is true throughout the program.
  • Exception-Handling related Tutorial Article

    1. Java Tutorial : Exception Handling
    2. Java Tutorial : Exception Classes
    3. Java Tutorial : Built-in Exceptions
    4. Java Tutorial : Implementing Exception Handling
    5. Java Tutorial : Implementing Exception Handling – Part 2
    6. Java Tutorial : Throwing An Exception – Part 1
    7. Java Tutorial : Throwing An Exception – Part 2
    8. Implementing User Defined Exceptions
    9. Java Tutorial : Assertion In Java

    Tags: , , , , , ,


    Oct 12 2009

    Java Tutorial : Built-in Exceptions

    Category: Javaalexi @ 11:44 pm

    The built-in exceptions in Java are categorized on the basis of whether the exception is handled by the Java compiler or not. Java consists of the following categories of built-in exceptions:

    • Checked Exceptions
    • Unchecked Exceptions

    Checked Exceptions

    Checked exceptions are the objects of the Exception class or any of its subclasses excluding the Runtime Exception class. Checked exceptions are the invalid conditions that occur in a Java program due to invalid user input, network connectivity problem, or database problems. For example, java.io.IOException is a checked exception. The IOException exception is thrown whenever an input/output operation is abnormally terminated.

    Java uses the try-catch block to handle the checked exceptions. The statements within a program that throw an exception are placed in the try block. You associate an exception-handler with the try block by providing one or more catch handlers immediately after the try block.

    The following table lists the various checked exceptions defined in the java.lang package:

    Exception

    Cause of Creation

    ClassNotFoundException

    Occurs when the Java run-time
    system is unable to find the class
    referred.

    IllegalAccessException

    Occurs when you want to refer a
    class that is not accessible.

    InstantiationException

    Occurs when you try to create an
    object of an abstract class or
    interface.

    NoSuchMethodException

    Occurs when you call a method that
    does not exist.

     

    Unchecked Exceptions

    Unchecked exceptions are the run-time errors that occur because of programming errors, such as invalid arguments passed to a public method. The Java compiler does not check the unchecked exceptions during program compilation. For example, if you divide a number by zero, an unchecked or run-time exception is raised.

    The following table lists the various unchecked exceptions:

    Exception

    Cause of Creation

    ArithmeticException

    Occurs when you make an
    arithmetic error, such as dividing a
    number by zero.

    ArrayIndexOutOfBoundsException

    Occurs when an attempt is made
    to access an array element beyond
    the index of the array.

    ArrayStoreException

    Occurs when you assign an
    element to an array that is not
    compatible with the data type of
    that array.

    ClassCastException

    Occurs when you assign a
    reference variable of a class to an
    incompatible reference variable of
    another class.

    IllegalArgumentException

    Occurs when you pass an
    argument of incompatible data
    type to a method.

    NegativeArraySizeException

    Occurs when you create an array
    with negative size.

    NullPointerException

    Occurs when an application tries to
    use an object without allocating
    memory to it or calls a method of
    a null object.

    NumberFormatException

    Occurs when you want to convert
    a string in an incorrect format to a
    numeric format.

    Tags: , , ,