throw and throws keyword

PHOTO EMBED

Thu Jan 18 2024 07:04:01 GMT+0000 (Coordinated Universal Time)

Saved by @E23CSEU1151 #java

class D
{
    void div(int a, int b) throws ArithmeticException
    {
        if (b == 0)
        {
            throw new ArithmeticException();
        }
        else
        {
            int c = a / b;
            System.out.println(c);
        }
    }

    public static void main(String[] args)
    {
        D r = new D(); // Fix the typo here
        try
        {
            r.div(10, 0);
        }
        catch (Exception e)
        {
            System.out.println("We cannot divide by 0");
        }
    }
}
content_copyCOPY