java exception

Exception

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally,
which is not recommended, therefore, these exceptions are to be handled.
The Java programming language uses exceptions to handle errors and other exceptional events.
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
Exception handling is powerful mechanism that handles runtime errors to maintain normal application flow
Exception can occur for many reasons.
example:
A user has entered invalid data.
A file not exist while trying to open it.
A network connection has been lost in the middle of communications.
Insufficient Memory.
Exception Handling
Exception can be handled by using the Java Exception Handling Keywords
try
catch
finally
throw
throws

Exception example program
import java.io.File;
import java.io.FileReader;
public class FilenotFound {
public static void main(String args[]) {
File file = new File(“E://file.txt”);
FileReader f = new FileReader(file);
}
}

This example containing exception because file not exist or found.
To handle such Exception or error we use some methods. such as.

try {
// Protected code
}catch(ExceptionName e1) {
// Catch block
}
Exception handling program
import java.io.*;
public class Excephandle {
public static void main(String args[]) {
try {
int a[] = new int[2];
System.out.println(“Access element three :” + a[3]);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(“Exception thrown :” + e);
}
System.out.println(“Out of the block”);
}
}
Multiple catch block
try {
// Protected code
}catch(ExceptionType1 e1) {
// Catch block
}catch(ExceptionType2 e2) {
// Catch block
}catch(ExceptionType3 e3) {
// Catch block
}
Exception handling program by using Throws block
import java.io.*;
public class Exp {
public void deposit(double amount) throws RemoteException {
// Method implementation
throw new RemoteException();
}
// Remainder of class definition
}
Try , multi catch and finally block

try {
// Protected code
}catch(ExceptionType1 e1) {
// Catch block
}catch(ExceptionType2 e2) {
// Catch block
}catch(ExceptionType3 e3) {
// Catch block
}finally {
// The finally block always executes.
}

Finally keyword Exception handling program
public class ExcepHandle{
public static void main(String args[]) {
int a[] = new int[2];
try
{
System.out.println(“Access element three :” + a[3]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(“Exception thrown :” + e);
}
finally
{
a[0] = 6;
System.out.println(“First element value: ” + a[0]);
System.out.println(“The finally statement is executed”);
}
}
}
Exception handling program for file not exist.
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadData {
public static void main(String args[]) {
FileReader fr = null;
try
{
File file = new File(“file.txt”);
fr = new FileReader(file); char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); // prints the characters one by one
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
fr.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
}

BACK

NEXT