Throwing our Own Exceptions in Java

                         
                    
Throwing our exceptions:

There may be times when we would like to throw our exception we can do this by using the keyword throw as follows.
 
          Throw new throwable subclass;

Ex :  throw new ArithmeticException ( );

         throw new NumberFormatException ( ) ;

Note that exception is a subclass of throw able and therefore myException is a subclass of throw able class. A object of a class that extends throwable can be thrown and caught.

import java .lang. Exception;
class myException (String message)
{
 super  (message);
 }

  class testMyException
  {

  public static void main (String args[ ] )
   {
     int x= 5,  y=100;
          try
          {
           float z =(float) x/(float)y ;
           if(z < 0.01)
           {
            throw new myException (“ number is too small) ;
            }
          }
            catch ( myException e)
           {
            System.out.println (“ caught my exception”);
            System.out.println (e. getMessage ( ) );
            }
            finally
            {
            System.out.println (“ I am always here”);
            }
      }
     }

The object which contains the error message.“Number is too small” is caught by catch block. which then displays the message using the        getMessage ( ) method.                                                                    



Related

Features of Java

Java is  simple  and  familiar   program  language:- learning and practicing java  is easy because it contains many features of c and c++. Java is  s...

Explain Data Types in Java?

Every  variable  in java  has  a  data type.  Data  type specify the  size of the memory occupied   and  type  of   va...

Program for Demonistrating LIFE CYCLE OF APPLET

import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.applet.*; public class App extends Applet implements ActionListener {       &nb...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item