0% found this document useful (0 votes)
12 views8 pages

Java Updated After20

The document discusses Java code examples for exception handling, multithreading, and thread priorities. It includes try/catch blocks, custom exceptions, extending the Thread class, setting thread priorities, and using sleep methods.

Uploaded by

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

Java Updated After20

The document discusses Java code examples for exception handling, multithreading, and thread priorities. It includes try/catch blocks, custom exceptions, extending the Thread class, setting thread priorities, and using sleep methods.

Uploaded by

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

Practical 25

import java.util.Scanner;
import java.lang.Exception;

class A extends Exception {


A(String msg) {
super(msg);
}
}

class B {
public static void main(String args[]) {
System.out.println("Enter the Password");
Scanner sc = new Scanner(System.in);
String Pass;
Pass = sc.nextLine();
try {
if (!Pass.equals("ABCD"))
throw new A("Authentication Failure");
else
System.out.println("Welcome");
} catch (A e) {
System.out.println(e.getMessage());
}
}
}
Practical 26

import java.util.Scanner;

class throwown1 extends Exception {


throwown1(String msg) {
super(msg);
}
}

class thowAge {
public static void main(String[] args) {
System.out.println("Enter your age");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
try {
if (age < 0)
throw new throwown1("Your age is negative");
else
System.out.println("Correct age");
} catch (throwown1 e) {
System.out.println("This is My Exception Block {Executed}");
System.out.println(e.getMessage());
}
}
}
25 Practical
public class ex2

public static void main(String args[]) }

try

int a[]=new int[5]; System.out.println(a[10]);

} catch(ArithmeticException e)

System.out.println("Arithmetic Exception occurs"); }

catch(ArrayIndexOutOfBoundsException e)

System.out.println("ArrayIndexOutOfBoundsException occurs");

} finally

System.out.println("program end");

}
}
}
Prac 21
class A extends Thread
{
public void run()
{
System.out.println("Even no Thread:");
try
{
for(int i=1;i<=10;i++)
{
if(i%2==0)
System.out.println(i);
Thread.sleep(500);
}
}
catch(InterruptedException e){}
}
}
class B extends Thread
{
public void run()
{
System.out.println("Odd no Thread:");
try
{
for(int i=1;i<=10;i++)
{
if(i%2!=0)
System.out.println(i);
Thread.sleep(500);
}
}
catch(InterruptedException e){}
}
}
class Threading
{
public static void main(String args[])
{
A a1=new A();
a1.start();
try
{
a1.join();
}
catch(InterruptedException e){}
B b1=new B();
b1.start();
}
}
Prac 22
class A extends Thread
{
public void run()
{
System.out.println("Thread1 Execution Started");
System.out.println("Thread1 Termination");
}
}
class B extends Thread
{
public void run()
{
System.out.println("Thread2 Execution Started");
System.out.println("Thread2 Termination");
}
}
class C extends Thread
{
public void run()
{
System.out.println("Thead3 Execution Started");
System.out.println("Thread3 Termination");
}
}
class Priority
{
public static void main(String args[])
{
A obj=new A();
B obj1=new B();
C obj2=new C();
obj.setPriority(Thread.MAX_PRIORITY);
obj.setPriority(Thread.NORM_PRIORITY);
obj.setPriority(Thread.MIN_PRIORITY);
obj.start();
obj1.start();
obj2.start();
}
}

You might also like