Practical 21


Aim:- Write a program to implement Custom Exception in java.

JAVA Code :-

import java.lang.*;

class MyException extends Exception
{
            int a;

            MyException(int b)
            {
              a=b;
            }

            public String toString()
        {
                        return "Hello its Custom Exception";
            }
}

class CustomException
{
            static void compute(int a) throws MyException
            {
                if(a>10)
                {
                        throw new MyException(a);
                 }
                        System.out.println("Normal exit");
            }
           
            public static void main(String args[])
            {
                try
                {
                        compute(1);
                        compute(11);
                 }
           
                 catch (MyException e)
                 {
                        System.out.println("Caught: " + e);
                  }
            }
}

Output:-



No comments:

Post a Comment

Programs

Home Aim:- Write a program to convert rupees to dollar. (60rupees=1 dollar). Aim:- Write a program that calculate percentage marks...