0% found this document useful (0 votes)
1 views2 pages

Additional Programs

The document contains two Java programs. The first program defines a method that checks if a number is odd and throws an exception if it is, while the second program checks if a thread is alive before and after starting it. Both programs demonstrate basic exception handling and thread management in Java.

Uploaded by

Santhosh B J
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)
1 views2 pages

Additional Programs

The document contains two Java programs. The first program defines a method that checks if a number is odd and throws an exception if it is, while the second program checks if a thread is alive before and after starting it. Both programs demonstrate basic exception handling and thread management in Java.

Uploaded by

Santhosh B J
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

1) Write a Java program to create a method that takes an integer as a parameter and throws an

exception if the number is odd.

public class Exception_OddNumber {


public static void main(String[] args) {
int n = 18;
trynumber(n);
n = 7;
trynumber(n);
}

public static void trynumber(int n) {


try {
checkEvenNumber(n);
[Link](n + " is even.");
} catch (IllegalArgumentException e) {
[Link]("Error: " + [Link]());
}
}

public static void checkEvenNumber(int number) {


if (number % 2 != 0) {
throw new IllegalArgumentException(number + " is odd.");
}
}
}

Write a Java program to check thread is alive or not


public class CheckThread
{
public static void main(String[] args)
{
Thread t = new Thread(new MyThread());

[Link]("Thread isAlive : " + [Link]());


[Link]();
[Link]("Thread isAlive : " + [Link]());
}
}
class MyThread implements Runnable
{
public void run()
{
try
{
[Link]("Thread is running.");
} catch (Exception e)
{
[Link](e);
}
}
}

You might also like