Java Unit - 3(programs)
Java Unit - 3(programs)
package myfirstproject;
class practical
{
public static void main(String[] args)
{
int a= 10;
int b =0;
try
{
int result = a / b;
System.out.println("Result : "+result);
}
catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)// We can Write Multiple Catch blocks to catch the exception proplerly
{
System.out.println(e.getMessage());
}
}
}
package myfirstproject;
class practical
{
public static void main(String[] args)
{
int a= 10;
int b =0;
try
{
int result = a / b;
System.out.println("Result : "+result);
}
catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}
}
}
package myfirstproject;
class practical
{
public static void main(String[] args)
{
int[] a = {10,20,30,40,50};
try
{
System.out.println("7th element of array : "+a[-1]);// throw exception because no
-1th index at array
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
}
}
Ex : -
● Write a program that throws an exception called 'NoMatchException' when a string is not
equal to 'India'. (exp 12)
import java.util.*;
class NoMatchException extends Exception
{
NoMatchException(String s)
{
super(s);
}
}
class practical
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter String : ");
String s = sc.nextLine();
try
{
check(s);
}
catch(NoMatchException e)
{
System.out.println(e.getMessage());
}
}
public static void check(String s) throws NoMatchException
{
if(s.equals("India"))
{
System.out.println("Correct !");
}
else
{
throw new NoMatchException("Incorrect ! not India");
}
}
}
4. Thread
package myfirstproject;
class myThread implements Runnable
{
public void run()
{
System.out.println("Thread is Running !");
}
}
class practical
{
public static void main(String[] args)
{
myThread t = new myThread();
Thread t1 = new Thread(t);
t1.start();
}
}
5. Multi Threading : in java, multiple threads can execute at the same time
Ex :
● Write a program to create two threads. One thread will display the odd numbers from 1
to 50 and the other thread will display the even numbers. (exp 11)
package myfirstproject;
class even extends Thread
{
public void run()
{
for(int i = 1;i<=50;i++)
{
if(i%2==0)
{
System.out.println("Even : "+i);
}
}
}
}
class odd extends Thread
{
public void run()
{
for(int i = 1;i<=50;i++)
{
if(i%2!=0)
{
System.out.println("Odd : "+i);
}
}
}
}
class practical
{
public static void main(String[] args)
{
even e = new even();
odd o = new odd();
e.start();
o.start();
}
}
6. Thread Priority - we can Assign priority to our thread , that means High priority task will
execute first ;
package myfirstproject;
class myThread extends Thread
{
public void run()
{
System.out.println("Thread is Running !");
}
}
class practical
{
public static void main(String[] args)
{
myThread t = new myThread();
// set priority to thread
t.setPriority(Thread.MAX_PRIORITY);
System.out.println("Priority of thread : "+t.getPriority());
t.setPriority(Thread.MIN_PRIORITY);
System.out.println("Priority of thread : "+t.getPriority());
t.setPriority(Thread.NORM_PRIORITY);
System.out.println("Priority of thread : "+t.getPriority());
}
}