java program to Handling Exceptions with try-catch.

 
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try {
            System.out.print("Enter a number: ");
            int number = scanner.nextInt();
            System.out.println("You entered: " + number);
        } catch (Exception e) {
            System.out.println("Invalid input.");
        }

        System.out.println("Program continues after exception handling.");
    }
}

java program to Handling Multiple Exceptions

 
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try {
            System.out.print("Enter a number: ");
            int number = scanner.nextInt();
            System.out.println("You entered: " + number);

            int result = 10 / number;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Error: Cannot divide by zero.");
        } catch (Exception e) {
            System.out.println("Error: Invalid input. ");
        }

        System.out.println("Program continues after exception handling.");
    }
}


java program to Handling Exceptions Using finally Block

 
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try {
            System.out.print("Enter a number: ");
            int number = scanner.nextInt();
            System.out.println("You entered: " + number);

            int result = 10 / number;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Error: Cannot divide by zero.");
        } finally {
            System.out.println("Finally block executed.");
        }

        System.out.println("Program continues after exception handling.");
    }
}


java program to creating Custom Exceptions.

 

// Define a custom exception class
class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            int age = 15;
            



            if (age < 18) {
               
                throw new CustomException("You must be at least 18 years old.");
            } else {
                System.out.println("Access granted.");
            }
        } catch (CustomException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}


java program to Creating and Running a Thread.

 
public class MyThread extends Thread {
    public void run() {
        System.out.println("Thread is running.");
    }

    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
    }
}


java program to Implementing Runnable Interface.

 
public class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Runnable is running.");
    }

    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start();
    }
}

java program of Synchronization and Inter-thread Communication.

 
// Define an interface
interface Animal {
    void sound();
}

abstract class AbstractAnimal implements Animal {
    abstract void eat();
}

class Dog extends AbstractAnimal {
    void sound() {
        System.out.println("Dog barks");
    }

    void eat() {
        System.out.println("Dog eats bones");
    }
}

public class Main {
    public static void main(String[] args) {
        // Create an object of the Dog class
        Dog dog = new Dog();

        // Call the methods 
        //defined in the Animal interface
        dog.sound();

        // Call the method defined in the
        // AbstractAnimal abstract class
        dog.eat();
    }
}


index