NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Showing posts with label Exception Handling. Show all posts
Showing posts with label Exception Handling. Show all posts

Sunday, October 21, 2012

Java program to create user defined exception handling


import java.io.*;
class myException extends Exception
{ myException()
  { System.out.println("Error:Password too short");
  }
  myException(int n)
  { System.out.println("Error:Only adults can join");
  }
}
class user_exception
{ public static void main(String s[])throws IOException,myException
  { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    try
    { System.out.print("Enter user name : ");
      String n=br.readLine();
      System.out.print("Enter your password : ");
      String m=br.readLine();
      if(m.length() <6)
        throw new myException();
      System.out.print("Enter your age : ");
      int o=Integer.parseInt(br.readLine());
      if(o<18)
        throw new myException(o);
    }
    catch(Exception e)
    {    }
  }
}   
Output

Java program to illustrate exception handling


import java.lang.ArithmeticException;
import java.util.*;                                //import util package
public class ExceptionDemo
{  public static void main(String s[])
  {   Scanner b1=new Scanner(System.in);
    try
     {  System.out.println("Enter the Divident?");
        double a=b1.nextDouble();
        System.out.println("Enter the Divisor?");
        double b=b1.nextDouble();
      if(b==0)
  //Throwing an Exception using throw
       throw new ArithmeticException("Division by zero!");
       double res=a/b;
       System.out.println("Result of"+a+"/"+b+"is"+res);
     }
  //Handles input mismatch exception caused by
 //entering a non-integer value at th point
  catch(InputMismatchException e)
       {  System.out.println("Error:"+e);}
//Handles the divide-by-zero ArithmeticException
  catch(ArithmeticException ae)
      {  System.out.println("Error:"+ae);}
   }
}
Output

Sunday, August 12, 2012

Java program to Implement Runnable class


class childthread implements Runnable
{
Thread t;
childthread()
{
t=new Thread(this,"odd");
System.out.println("child created "+t);
t.start();
}
public void run()
{
try
{
for(int i=1;i<10;i++)
 {
 System.out.println("child "+i);
 i++;
 Thread.sleep(500);
 }
}
catch(InterruptedException e)
{}
}
}
class oddeventhread
{
public static void main(String[] s)
{
 childthread c= new childthread();
try
{
 for(int i=2;i<=10;i++)
  {
   System.out.println("even "+i);
   i++;
   Thread.sleep(1000);
  }
}
catch(InterruptedException e)
{}
}
}

Program to do Exception Handling in java


import java.lang.Throwable;
class exception
{
public static void main(String[] args)
{
int c;
int a=5;
int b=0;
try
{
System.out.println("Hellow....");
c=a/b;
System.out.println("Haiii");
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("yippeeeee");
}
}