Search This Blog

#3. Exceptions Handling in Java

Concepts :

  • what is a Exceptions?
  • what is a Exception Handling?
  • Exception in Java?
  • How many type of Exceptions?
  • Blocks in Exceptions?
  • when we need it in programming?
  • Example Source Codes :

What is a Exception?


                                   Exception is an abnormal condition at run-time. In general terms, Exception is an constraint or condition which we usually not to do that. Same thing comes into Java programming also.

Best example for Exception is "Division of some number with zero, is it possible?"

According to Fundamentals of Mathematics, Division of some number with zero is not possible. It is condition or constraint.


Exception in Java?


                                    Exception is a Base Class in java. It contains method and sub classes .All the exception types are sub-classes of the built-in class.

Methods:

1.  Signature : public String getMessages()
     System.out.pritnln(ae.getMessages())
2. Signature : public String toString()
    System.out.println(ae.toString());
3. signature : public String printStackTrace();
    System.out.println(ae.printStackTrace());

What is a Exception Handling?


                                     An Exception is a run-time error. In computer languages that do not support exception handling, errors must be checked and handled manually-typically through the use of error codes. In cases of Java programming, we can execute the entire program without any termination, but at the end of the program you will get the exception.

How to handle the Exception ?

For this Question, we have the Blocks.

Blocks :


  1. Try
  2. Catch
  3. Throw
  4. Throws
  5. Finally
Try : It is First Block in the source code. It contains Business Logic(set of statements) if it is executed successful then there is no problem otherwise control goes into another block i.e. catch
Catch : In this block, we need to catch the Exception which was going to occur at the Run-time.
Throw : It is possible for your program to throw an explicitly, using the throw statement.
Throws : If you are unable to handle Exception explicitly then you can throw the Exception by using throws.
Finally : It will execute after the try/catch blocks whether it is executed successfully or not.


How many types of Exceptions?

  • Predefined Exceptions or Built-in Exception.
      • Checked Exceptions
      • Unchecked Exceptions
  • User defined Exceptions. 
Built-in Exception : 

Checked Exceptions :

        Exception
        ClassNotFoundException
        CloneNotSupportedException
        IllegalAccessException
        InstantiationException
        InterruptedException
        NoSuchFieldException
        NoSuchMethodException
         
    Unchecked Exceptions :


          Exception
          ArithmeticExaception

          ArrayIndexOutOfBoundsException

          ArrayStoreException

          ClassStoreException

          IllegalArgumentException

          IllegalMonitorStateException

          IllegalStateException

          IllegalThreadStateException

          IndexOutOfBoundsException

          NegativeArraySizeException

          NullPointerException

          NumberFormatException

          SecurityException

          StringIndexOutOfBounds

          TypeNotPresentException

          UnsupportedOperationException

    No comments:

    Post a Comment