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

2033 Java Prac 8

Uploaded by

omkar23cs
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

2033 Java Prac 8

Uploaded by

omkar23cs
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/ 4

Name : Omkar Dhanavade

Roll No : 2033

Practical 8

8A] Write a java program to implement exception handling.

Code:

import java.util.Scanner; public

class ExceptionOdd { public

static void chknum(int n)

try

checkEvenNumber(n);

System.out.println(n + " is even.");

catch (IllegalArgumentException e)

System.out.println("Error(entered number is under exception) " + e.getMessage());

public static void checkEvenNumber(int number)

if (number % 2 != 0)

throw new IllegalArgumentException(number + " is odd.");

}
Name : Omkar Dhanavade
Roll No : 2033

public static void main(String[] args)

Scanner sc= new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt(); chknum(n);

Output :

C:\Users\student\Desktop>javac ExceptionOdd.java

C:\Users\student\Desktop>java ExceptionOdd

Enter a number

19

Error (entered number is under exception) 19 is odd

8 B] Write a java program to implement the concept of multithreading.

Code :

import java.util.Scanner;

class MultithreadingDemo extends Thread {

public void run() {

try {

System.out.println(

"Thread " + Thread.currentThread().getId() + " is running");

} catch (Exception e) {

System.out.println("Exception is caught");
Name : Omkar Dhanavade
Roll No : 2033

public class EnhancedMultithread {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of threads: ");

int n = scanner.nextInt(); for (int i = 0; i < n; i++) {

MultithreadingDemo object = new MultithreadingDemo();

object.start();

scanner.close();

Output :

C:\Users\student\Desktop> javac EnhancedMultithread.java

C:\Users\student\Desktop> java EnhancedMultithread

Enter the number of threads : 4

Thread 10 is running

Thread 11 is running

Thread 12 is running

Thread 13 is running

You might also like