Professional Documents
Culture Documents
Exception in Java
Exception in Java
ClassNotFoundException,
IOException,
SQLException,
RemoteException, etc
Example:
1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5;//exception occurs
6. statement 6;
7. statement 7;
8. statement 8; Rest of the Code
9. statement 9;
10. statement 10;
Exception Example:
-----------------------------------------------------------------------------------
Divide(int a) throws ArithmeticException
int a=50/0;//ArithmeticException
String s=null;
System.out.println(s.length());//NullPointerException
---------------------------------------------------------------------------
String s="abc";
int i=Integer.parseInt(s);//NumberFormatException
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Eg:
3) Error
Error is irrecoverable.
Example Code1:
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
//rest code of the program
System.out.println("rest of the code...");
}
}
Example Code2:
public class MultipleCatchBlock1 {
public static void main(String[] args) {
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
not.
Therefore, it contains all the necessary statements that need to be
public class TestFinallyBlock1{
public static void main(String args[]){
try {
System.out.println("Inside the try block");
//below code throws divide by zero exception
int data=25/0;
System.out.println(data);
}
//cannot handle Arithmetic type exception
//can only accept Null Pointer type exception
catch(NullPointerException e){
System.out.println(e);
}
//executes regardless of exception occured or not
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}
EXCEPTION : Throw
Throws:
Syntax:
Example:
/*
*/
package arraylistcustomerdb;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
class Customer {
return id;
this.id = id;
return name;
this.name = name;
return salary;
this.salary = salary;
return address;
this.address = address;
}
public class ArrayListCustomerDB
Class.forName("com.mysql.jdbc.Driver");
ResultSet rs;
rs = stmt.executeQuery(sql);
while (rs.next())
customer.setId(rs.getString("id"));
customer.setName(rs.getString("name"));
customer.setAddress(rs.getString("address"));
customer.setSalary(rs.getString("salary"));
customerList.add(customer);
return customerList;
try {
System.out.println(customerlist.get(i).getId());
System.out.println(customerlist.get(i).getName());
System.out.println(customerlist.get(i).getSalary());
System.out.println(customerlist.get(i).getAddress());