Experiment no:5
Aim 5(a): Write a Java program to implement user defined exception handling for negative
amount entered.
Code:
import [Link].*;
class NewException extends Exception{
public NewException(float n)
[Link](n + " is a negative value");
public class exception{
public static void main(String args[])
Scanner sc = new Scanner([Link]);
[Link]("Enter the amount : ");
try{
float amt = [Link]();
if(amt<0)
throw new NewException(amt);
[Link]("You enter : "+amt);
catch(NewException ex)
[Link]("Exception");
Output:
5(b): Write a program in java which creates two threads, “Even” thread and “Odd” thread
and print the even no using Even Thread after every two seconds and odd no using Odd
Thread after every five second.
Code:
class odd extends Thread{
public void run(){
for(int i=1;i<10;i+=2)
[Link]("Odd number : "+i);
try{
[Link](5000);
catch(InterruptedException e)
[Link](e);
class even extends Thread{
public void run(){
for(int i=0;i<10;i+=2)
[Link]("Even number : "+i);
try{
[Link](5000);
catch(InterruptedException e)
{
[Link](e);
class thread{
public static void main(String args[])
odd obj1 = new odd();
even obj2 = new even();
[Link]();
[Link]();
Output: