0% found this document useful (0 votes)
3 views

Java Unit - 3(programs)

The document covers Java exception handling using try-catch blocks, including built-in exceptions like ArithmeticException and ArrayIndexOutOfBoundsException, as well as creating custom exceptions. It also discusses thread creation methods, multi-threading, and thread priority management in Java. Sample code snippets illustrate each concept, demonstrating practical applications of exception handling and threading.

Uploaded by

aditishirole2112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Unit - 3(programs)

The document covers Java exception handling using try-catch blocks, including built-in exceptions like ArithmeticException and ArrayIndexOutOfBoundsException, as well as creating custom exceptions. It also discusses thread creation methods, multi-threading, and thread priority management in Java. Sample code snippets illustrate each concept, demonstrating practical applications of exception handling and threading.

Uploaded by

aditishirole2112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Unit - 3

1.​ Try - Catch Block

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());
​ ​ }
​ }
}

2.​ Built in exceptions :

●​ Arithmetic Exception - when we try to do wrong arithmetic operation

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());
​ ​ }
​ }
}

●​ ArrayIndexOutOfBoundsException - when we try to access element beyond


array size

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());
​ ​ }
​ }
}

3.​ Custom Exception - We can Create our own Exceptions

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

●​ There are two ways to create thread

1.​ By extending Thread class


​ ​
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();
​ ​ t.start();
​ }
}

2.​ By implementing Runnable interface

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());
​ ​
​ }
}

You might also like