JAVA PROGRAMS || EXCEPTION HANDLING

JAVA PROGRAMS 



JAVA PROGRAMS || EXCEPTION HANDLING





EXCEPTION HANDLING

The error which comes  during run time and handle by java and any other programming language its called exception handling.


ERROR

  • COMPILE TIME ERROR
  • LOGICAL ERROR
  • RUNTIME ERROR

EXCEPTION

  • inbuilt exception
  • user defined exception

INBUILT EXCEPTION


  • implicit
  • explicit
implicit - exception handle by java.

e.g- class Demo
       {
       public static void main(String args[])
{
  int a=10,b=0,c=a/b;
}
}
there are two type of implicit exception in java.
  • checked exception
  • unchecked exception
unchecked Exception -

import java.lang

-->Arithmatic Exception
-->ArrayIndexOutOfBoundException
-->NullPointerException
-->NegativeArraySizeException
-->NoSuchMethodException
-->NOSuchClassFoundException

explicit - 

there are five keyword in exception handling.

  • try
  • catch
  • throw
  • throws 
  • finally
try- in try block exception can be detected.

catch - in catch block exception can be handle.

throw- the throw statement is used to explicitly throw a exception. in try block detect exceptions and throw into catch block this process called throw.

throws- throws genrate list of exception in try block and throw one by one.

finally - finally block will be exicuted either exception is genrated or not genrated. finally block is depend upon the activity of try and catch block.
e.g -

try
{
}
catch(-----)
{
}

finally
{
}

example -

class Demo
{
public static void main(String args[])
{
try
{
int a=10,b=0;c;
c=a/b;
catch(arithmetic exception )
{
System.out.println(e);
}
finally
{
System.out.println("welcome");
}
}
USER DEFINED EXCEPTION

  • checked Exception
  • sql Exception
  • I/O Exception
  • servlet Exception

EXCEPTION HANDLING EXAMPLES OR QUESTIONS

Q1)Define a class MyData (Day, Month, year) with methods to accept and display a
MyData object Accept data as dd, mm, yyyy. Throw user defined execution
“InvalidDateException” if the data is invalid.
Example of invalid dates:
a. 12 15 2015
b. 31 6 1990
c. 29 2 2015

--->

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



class InvalidDateException extends Exception{
}
public class MyDate {

int dd,mm,yy;

public MyDate(){
}
public MyDate(int dd,int mm,int yy) {

this.dd=dd;
this.mm=mm;
this.yy=yy;
}
public boolean accept(){
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter Day:\t");
dd=Integer.parseInt(br.readLine());
System.out.println("Enter Month:\t");
mm=Integer.parseInt(br.readLine());
System.out.println("Enter Year:\t");
yy=Integer.parseInt(br.readLine());
} catch (Exception  e) {

e.printStackTrace();
}
try{
if(dd<=0 || dd>=32) throw new InvalidDateException();
if(mm<=0 || mm>=13) throw new InvalidDateException();
if(dd== 29 && mm==2 && (yy%4)!=0 ) throw new InvalidDateException();
}
catch(InvalidDateException e){
System.out.println("Inavlid Date: \t"+dd+"/"+mm+"/"+yy);
return false;
}
return true;
}

public static void main(String[] args) {

MyDate d=new MyDate(); 
String str=null;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do{
if(d.accept())d.display();
System.out.println("You want Enter new date(y/n)?");
try {
str=br.readLine();
} catch (IOException e) {

e.printStackTrace();
}
}while(str.equalsIgnoreCase("y"));
}
private void display() {

System.out.println(dd+"/"+mm+"/"+yy);
}

}

---------------------------------------------------------------------------------

Q2)Define a class SavingAccount (acno, name, balance). Define appropriate
constructors and operations withdraw(), deposit(), and viewbalance(). The
minimum balance must be 500. Create an object and perform operations. Raise user
defined “InsufficientFundsException” when balance is not sufficient for withdraw
operation.

--->

import java.io.*;


class InsufficientFundsException extends Exception
{

}

 class SavingAccount
 {
   
    int acNo;
    String name;
    int bal;
  
  SavingAccount(int n,String na,int b)
 {
    this.acNo=n;  
    this.bal=b;
    this.name=na;
  }
  void viewCreditAmount()
  {
  

     System.out.println("Balance is:--"+this.bal);
   }
   
  void withdraw(int add)
  {
       try
      {
       bal-=add;

       if(bal<500)
      {
        bal+=add;
        throw new  InsufficientFundsException();

      }
      }
       catch(Exception e)
       {
       System.out.println("balance is not sufficient for withdraw operation");
       }

       viewCreditAmount();
 }
  
  void deposit(int add)
{  
  bal+=add;
  viewCreditAmount();
 }
  
  
  
   void show()

 {
  System.out.println("Name is:--"+this.name);
  System.out.println("No is:--"+this.acNo);

  viewCreditAmount();
 }
  public static void main(String args[])throws IOException
  {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter no of objects:--");
    int r=Integer.parseInt(br.readLine());
    for(int i=0;i<r;i++)
   {
     
     System.out.println("Enter name:--");
     String name=br.readLine();
     System.out.println("Enter no:--");
     int n=Integer.parseInt(br.readLine());
     System.out.println("Enter Balance:--");

     try
   {
     
     int n1=Integer.parseInt(br.readLine());
     if(n1<500)
     {
     
       throw new InsufficientFundsException();
     }
    
     else
     {
       SavingAccount s=new SavingAccount(n,name,n1);
       System.out.println("Enter amount to deposite:--");
       int n5=Integer.parseInt(br.readLine());
       s.deposit(n5);
       System.out.println("\nEnter amount to withdraw:--");
       int n2=Integer.parseInt(br.readLine());
       s.withdraw(n2);
       s.show();
     }
   }
   catch(Exception e)
  {
   System.out.println("Balance is no sufficient:--");
  }
  }
 }
 }
---------------------------------------------------------------------------------------------

Q3)write a program in java to accept the value from user if the number is positive display positive number if the number is negative display negative number exception.

--->

import java.util.*;
import java.lang.*;

class Negativenumber extends Exception
{
}
class Demo
{
public static void main(String args[])
{
int a;
Scanner sc=new scanner(System.in);
System..out.println("Enter the value of a");
a=sc.nextInt();
try
{
if(a<0)
{
NegativeNumber n=new NegativeNumber();
throw n;
}
else
{
System.out.println("number is positive");
}
}
catch(NegativeNumber n)
{
System.out.println("number is negative");
}
}
----------------------------------------------------------------------------------------


Q4)write a program in java to accept the value from the user and Whether the given number is even and odd if it is even display even number otherwise display odd number  exception.

--->


import java.util.*;
import java.lang.*;
class Even extends Eception
{
}
class Demo
{
public static void main(String args[])
{
int a;
Scanner sc=new Scanner(Systen.in);
System.out.println("Enter a value of a");
a=sc.nextInt();
try
{
if(a%2==0)
{
System.out.println("number is even");
}
if(a%2==1)
{
even n=new even();
throw n;
}
}
catch(even n)
{
System.out.println("Number is odd");
}
}
--------------------------------------------------------------------------------------

Q5)write a program in java to accept the age from user if the age is greater then 18 is eligible for voting otherwise display age number exception.

---->


import java.util.*;
import java.lang.*;

class Number extends Exception
{
}
class Demo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter age");
age=sc.nextInt();
try
{
if(age>=18)
{
System.out.println("eligible for voting");
}
else
{
Number n=new Number();
throw n;
}
catch(Number n)
{
System.out.println("age number exception")
}
}

-------------------------------------------------------------------------------------------------

Q6)write a program to accept employee name from the user and check whether it is valid or not. if it is not valid then throw user defined exception name is invalid and display it.

--->

import java.util.*;

import java.lang.*;

class NameException extends Eception
{
}
class Demo
{
public static void main(String args[])
{
int i;

Scanner sc=new Scanner(Systen.in);
System.out.println("Enter a value of a");
name=sc.nextInt();
try
{
for(i=0;i<name.length();i++)
{
char c=name.charAt(i);
if(!(character.isLetter(c)))
{
NameException ne=new NameException();
throw ne;
}
}
System.out.println("name of employee ="+name);
}
catch(NameException ne)
{
System.out.println("invalid"+ne);
}
}
}
--------------------------------------------------------------------------------------------------

Q7)write a program in java to accept the number from the user if number is zero then throw user defined exception(NumberIsZero). otherwise calculate the sum of first and last digit of a given number.

--->

import java.util.*;

import java.lang.*;

class ZeroException extends Eception
{
}
class Demo
{
public static void main(String args[])
{
int n,first=0,last,sum=0;
Scanner sc=new scanner(System.in);
System.out.println("Enter number");
name=sc.nextInt();
try
{
if(n==0)
{
ZeroException ze=new ZeroException();
throw ze;
}
else
{
last=n%10;
first=n;
while(n>=10)
{
n=n/10;
}
first=n;
sum=first+last;
}
System.out.println("sum="+sum);
}
catch(ZeroException ze)
{
System.out.println("zero number exception");
}
}
}



Post a Comment

0 Comments