OOP lab 11
SOURCE
CODE:
import java.util.Scanner;
public class ExceptionDemo
{
public static void
main(String[] args)
{
Scanner input
= new Scanner(System.in);
int
donutCount, teaCount;
double
donutsPerGlass = 0;
try
{
System.out.println("Enter number of donuts:");
donutCount
= input.nextInt( );
System.out.println("Enter number of cup of tea:");
teaCount =
input.nextInt( );
if
(teaCount < 1)
//when
the throw statement is executed, the block is stopped throw new
Exception("Exception: No Tea!");
donutsPerGlass = donutCount/(double)teaCount;
System.out.println(donutCount + " donuts.");
System.out.println(teaCount + " cup of tea.");
System.out.println("You have " + donutsPerGlass + "
donuts for each cup of tea.");
}
catch(Exception
e) // catch block
{
System.out.println(e.getMessage( ));
System.out.println("Go buy some tea.");
}
System.out.println("End of program.");
}
}
SOURCE
CODE:
import java.util.Scanner;
import java.io.*;
public class TestExceptions
{
/* Divide by zero
error Exception Handling*/
static int
getInt() throws IOException
{
Scanner input
= new Scanner(System.in);
System.out.print("Enter an integer: ");
int s =
input.nextInt();
return
(s);
}
public static void
main(String[] args)
{
int n1=0,
n2=1, n3=0;
try
{
n1 =
getInt();
n2 = getInt();
n3 =
n1/n2;
}
catch
(Exception e)
{
System.out.println("[" + e + "]");
}
System.out.println(n1 + "/" + n2 + " = " + n3);
}
}
SOURCE
CODE:
public class Counter extends Thread{
int time;
public Counter(int
seconds)
{ time = seconds; }
public void run()
{ while(time > 0){
System.out.println(Thread.currentThread().getName()+"
"+time);
try { Thread.currentThread().sleep(1000);
time--;
} catch(InterruptedException ex){
System.out.println("error :"+ex.getMessage()); }
}
} }
public class mainthread {
public static void
main(String[]arfs)
{ Counter c1 = new Counter(10);
Counter c2 = new
Counter(4);
Counter c3 = new
Counter(2);
c1.start();
c2.start();
c3.start();
}}


Comments
Post a Comment