Java -Exceptional Handling with example

We already covered the topic Exception in our previous article ,Today I am going to share you some idea about Java Exception Handling

Exception Handling

Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often disrupting the normal flow of program execution.

Java Exception Handling with Example

throw:
The Java throw keyword is used to explicitly throw an exception.We can throw either checked or uncheked exception in java by throw keyword. The throw keyword is mainly used to throw custom exception.
The syntax of java throw keyword is given below.


throw new ArithmeticException("not valid");  

Example:


public class KnowledgefactoryThrowDemo {
static void validate(int age) {

if (age == 0) {

// explicitly throw an exception
throw new ArithmeticException("Invalid age");
} else if (age < 18) {
// explicitly throw an exception
throw new ArithmeticException("Not Eligible");
else if (age > 18) {
System.out.println("Eligible");
}
}

public static void main(String args[]) {

validate(0);
 }
}

Output:


Exception in thread "main" java.lang.ArithmeticException: Invalid age

at KnowledgefactoryThrowDemo.validate(KnowledgefactoryThrowDemo.java:5)
 at KnowledgefactoryThrowDemo.main(KnowledgefactoryThrowDemo.java:14)


throws:

The Java throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.
Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as IndexOutOfBoundsException, it is programmers fault that he is not performing check up before the code being used.
The syntax of java throws keyword is given below.


          return_type method_name() throws exception_class_name{  
          //method code  
          }  

Example:
Let's see the example of java throws clause which describes that checked exceptions can be propagated by throws keyword.

import java.io.IOException;

public class KnowledgefactoryThrowsDemo {
void m() throws IOException {
throw new IOException("device error");// checked exception
}

void n() throws IOException {
m();
}

void p() {
try {
n();
} catch (Exception e) {
System.out.println("exception handled");
}
}

public static void main(String args[]) {
KnowledgefactoryThrowsDemo obj = new                                                                                   KnowledgefactoryThrowsDemo();
obj.p();
System.out.println("normal flow...");
}
}

Output:

exception handled
normal flow...


try-catch:
Java try block is used to enclose the code that might throw an exception. It must be used within the method.
If an exception occurs at the particular statement of try block, the rest of the block code will not execute. So, it is recommended not to keeping the code in try block that will not throw an exception.
Java try block must be followed by either catch or finally block.

The syntax of java try-catch keyword is given below.

                     try {
// code that may throw an exception
           } catch (Exception e) {
        }


Example:   

public class KnowledgefactoryThrowsDemo {
public static void main(String[] args) {
try {
int data = 25 / 0; 
}
// handling the exception
catch (ArithmeticException e) {
System.out.println(e);
}
System.out.println("rest of the code");
}
}

Output:

java.lang.ArithmeticException: / by zero
rest of the code


finally:
finally block is optional and can be used only with try-catch block. Since exception halts the process of execution, we might have some resources open that will not get closed, so we can use finally block. finally block gets executed always, whether exception occurred or not.For more details please click the following link

Java -final-finally-finalize-in Java-with example




This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


What is Exception?please click the following link

Java-Exception-Heirarchy-types-example


Comments