!-- Global site tag (gtag.js) - Google Analytics --> Java Exceptions -programmingworld - programmingworld


Java Exceptions -programmingworld

                               Java  Exceptions

Java Exceptions are exceptional/unusual/abnormal events that occur during the execution of programs. To understand, what exceptions really are, consider the following simple program.

class java ExceptionTest1
public static void main(String s[])
int d=0,result;

Since division by 0 is invalid, we say that an Java exception occurs during the division. Consider the following array declaration.

int[] a=new int[4];
a[5]= 6;

since, any index out of the range 0 to 3, is invalid, an Java exception occurs when the second statement is executed. The following array declaration also results in a Java exception, since the size of an array can never be negative.

int b=new int [-3];

Consider the following class declarations:

class X {}

class Y extends X {}

With these declarations, the following typecast results in a java exception:

X x=new X();

Y y=new  (Y)x;

In the following example, since the string is null, length() method will cause an exception.

String s=null;

int len =s.length();

Since the following string contains characters, parseInt() method result in an java exception.

String s= "abc";

int i =Integer .parseInt(s);

                       Handling Java Exceptions

The Exception handling in Java is the powerful mechanism to handle the exception which occurs during the runtime. when a Java exception occurs during the execution of the code in the try block, JVM creates an exception object and declares that something wrong has happened. The java exception object contains a lot of useful debugging information such as a name of the thread, file name, a line of code at which java exception occurred, a name of java exception and It's description etc. The process of creating this object and disclosing it is called throwing the exception. Throwing a Java exception disrupts the normal flow of execution. The JVM then tries to find the code (called handler) that wants to handle (resolve ) the exceptional situation. If found, it passes the newly created object to this handler, which is said to be catching the exception. The handler is specified by catch block immediately after the try block as follows:

//code to inspect
//code to handle the java exception

  • The catch block declares a parameter that specifies the type of java exception it can handle.

class ExceptionTest
public static void main(String s[])
int d=0,result;
catch(ArithmeticException e)
System.out.println("An Arithmetic exception occurred.");
System.out.println("After catch block");

 five keywords are used for Java exception handling

1. Try               
2. Catch
3. Throw
4. Throws
5. Finally

                                       Types Of Exceptions


          There are two types of exceptions

  1. CheckedException
  2. UncheckedException    

       Most useful Exceptions and Error in Java Exception handling  


  • IOException

  • SQLException

  • ClassNotFoundException

  • RuntimeException

  1. ArithmeticException

  2. NullPointerException

  3. NumberFormatException

  4. IndexOutOfBoundsException

  • ArrayIndexOutOfBoundsException

  • StringIndexOutOfBoundsException


  • StackOverFlowError

  • VirtualMachineError

  • OutOfMemoryError


  Try & Catch:- 

                          Try & catch is used to handle an exception in java. If a programmer has doubt, this code generates an exception then they are writing this code inside the try & catch. you can see it on above example.


                       when a program generates an exception. which is not handled by the programmer, so use Throw, by using Throw programmer throws this exception to the operating system. And operating system handle this exception.


                   Finally, block is always executed. It does not depend on any exception when the program is run and any exception has generated another block of the program is not execute only finally block is executed.


  • Checked Exceptions   

Checked exceptions are those exceptions that must be caught or declared to be thrown. Java considers Throwable and any of its subclass that is not also a subclass of either RuntimeException or Error as checked exceptions. The Checked exception should be handled in the code to avoid compile-time error.  

  • Unchecked/Runtime Exceptions
Unchecked exceptions (also called runtime exception) are those which need not be caught or declared to be thrown explicitly. Note that checked and unchecked exceptions are functionally equivalent. We can do everything with checked exceptions that can also be done with unchecked exceptions, and vice versa.