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