Monday, June 20, 2016

Exception Handling Beginning Java Exapmle

Exception Handling

A software engineer commit many errors while developing software code.These errors are also called Bugs and the process of removing them is called debugging.Let us take a look at different type of bugs.

Errors in a Java Program

There are basically three types of errors in the Java program.


  1. Compile-time Errors
  2. Run-time Errors
  3. Exceptions
  • Compile-time Errors: These are syntactical errors found in the code due to which a program fails to compile.
Program:


/**
 * 
 */
package com.collectionpack;

/**
 * @author Abhinaw.Tripathi
 *
 */
public class CompileTimeErrorApp {

/**
* @param args
*/
public static void main(String[] args)
{
System.out.println("Hello")
System.out.println("Here is the error")
}

}

This program will fail to compile because there are no semi-colons at the end of System.out.println("Hello") .

Output: 

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error, insert ";" to complete BlockStatements
Syntax error, insert ";" to complete Statement

at com.collectionpack.CompileTimeErrorApp.main(CompileTimeErrorApp.java:17)

  • Run-time Errors:
  These errors are produced at run time because it represents the inefficiency of the computer system to execute  a particular statement.

Program:

/**
 * 
 */
package com.collectionpack;

/**
 * @author Abhinaw.Tripathi
 *
 */
public class RuntimeErrorApp {

/**
* @param args
*/
public static void main()
{
System.out.println("Run time Exception is here);

}

}

Output: NoSuchMethod Error:main

What happens if main() method is written without String args[] ?
Ans: The code will compile but JVM can not see the main() method without String args[] .

  • Exceptions:
    Basically, an exception is a  run-time error.Then there arises a doubt: Can not I call compile time error  an exception? The Answer is No. you can not call compile-time errors also exception.They come under errors.All exceptions occur only at run-time but some exception are detected compile time and some others at run-time.

The exception checked at compile time by Java compiler are called Checked Exception.
The exceptions that are checked by the JVM are called Unchecked Exception.

The unchecked exceptions and errors are considered as Unrecoverable and the programmer  can not do anything when they occur.
Such as IOException is an example for checked exception.So we threw it out  without handling it.This is done by throws clause .

All exception are declared as classes . in Java of course ,everything is a class in Java.Even errors are also represented by classes.

All these classes are descended from a super class called Throwable.

What is Throwable?
Ans: Throwable is a class that represents all errors and exceptions which may occur in Java.

Which is the super class for all exceptions and an error?
Ans: Exception is the super class of all exceptions in java.

What is the difference  between an exception and an error?
Ans: An exception is an error which can handled it.It means when an exception happens,the programmer can do something to handle it but an error which can not be handled.

Program: 

/**
 * 
 */
package com.collectionpack;

/**
 * @author Abhinaw.Tripathi
 *
 */
public class ExceptionAnErrorApp {

/**
* @param args
*/
public static void main(String[] args) 
{
      System.out.println("open file");
      int n=args.length;
      System.out.println("n= + " +n);
      int a =45/n;
      System.out.println("a "+a);
      System.out.println("Close File");
}

}

Output:

open file
n= + 0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.collectionpack.ExceptionAnErrorApp.main(ExceptionAnErrorApp.java:20)


No comments: