Error and Exception

Error : 

  • Mistake that cant be handle or recover using programming logics.
  • In java it is a sub-class of Throwable Interface.

Type of Errors:

  • Syntax Error
  • Logical Error
  • Runtime Error

Syntax Error:

Errors that arise to avoid  the rules or structure of the language have not been followed. They are detected by the compiler.

Logical Error:

Errors that occur when a program doesn't perform the way it was intended to.

Runtime Error:

Errors that occur while the program is running if the environment detects an operation that is impossible to carry out.


Some typical causes of errors:
  • Memory errors (i.e. memory incorrectly allocated, memory leaks,OutOfMemory)
  • File system errors (i.e. disk is full, disk has been removed)
  • Network errors (i.e. network is down, URL does not exist)
  • Calculation errors (i.e. divide by 0)

Exception:

It is the unexpected condition that arise during the program compilation or execution .
It terminates the program compilation or execution abnormally.
It can be handle using programming mechanism (exception handling).


Type of Exception:

  • Compile-Time Exception or Checked Exception
  • Run-Time Exception or UnChecked Exception

Compile-Time Exception(Checked Exception):



  • Exception that arise during the compilation of program,in this type of exception compiler forced to deal with exception .
  • It  inherited from the core Java class Exception. They represent exceptions that are frequently considered “non fatal” to program execution.
  • Checked exceptions must be handled in our code, or passed to parent classes for handling.

Run-Time Exception(UnChecked Exception):

  • Exception that arise during execution of program and that terminates execution flow abnormally.
  • It doesn't stop compiler to create .class(byte code).
  • It can be handle using exception handling mechanism.

Exception Hierarchy:





Some Checked Exception are:




Some Checked Exception are:





Exception Handling Mechanism:

Java provide some exception handling blocks and clause for handle the exception.

  These are............


  • try
  • catch
  • finally
  • throw 
  • throws

Use of exception handling mechanism:



In above method, generated exception is catchd inside catch block.



In above method  ,throws IO-Exception is used to declare exception and throw is used to throw exception.


Declaring exceptions:

  • Every method must state the types of checked exceptions it might throw. This is known as declaring exceptions.

     
    Example:
         public void myMethod()  throws IOException
         public void myMethod()  throws IOException, OtherException



Throwing exceptions:

When the program detects an error, the program can create an instance of an appropriate exception type and throw it. This is known as throwing an exception. 

Example:
        throw new TheException();
        TheException ex = new TheException();
        throw ex;
        
        or
         throw new Exception();


Catching exceptions:

  • It catch the generated exception and show the type of exception,line number where exception is generated,and file in which exception is generated using the method printStackTrace().


try {
  statements;  // Statements that may throw exceptions
}
catch (Exception1 e1) {
//  statements;
}
catch (Exception2 e2) {
//  statements;
}

catch (ExceptionN en) {
  //statements


}

//in Jdk-7 catch can have multiple-parameters
catch( final NullPointerException | ArithmeticException e)
{

e.printStackTrace();
}



Common Exception in Java Programming:

  • NullPointerException
  • ArithmeticException
  • NumberFormatException
  • FileNotFoundException
  • ClassNotFoundException
  • ArrayIndexOutOfBoundsException
  • etc..............

Exception Handling with method Overriding:

  The Overriding method can throw:

  • No exception
  • One or more of the exceptions thrown by the overridden method.



  The Overriding method cant throw:

  • Additional exceptions not thrown by the overridden method.
  • Super-Classes of the exceptions thrown by the overridden method.
  • It cant throw any checked exception when overridden method doesn't declare any exception,in this it allow to declare only unchecked exception.

..



*********************************************************************************
Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mobile: 9953584548
Write us at: - Smruti@apextgi.com and pratap@apextgi.com
..

No comments:

Post a Comment