DUCKING USING THROWS

Throws with Ducking(Implicitly Ducking)


Example 1:

 
                
             class ImplicitManager
             {
                public static void main(String []args)
                {
                   System.out.println(“Main Begin”);
                   test1();
                   System.out.println(“Main End”);
                }
                private static void test1()
                {
                   System.out.println(“test1 Begin”);
                   Test2();
                   System.out.println(“test1 End”);
                }
                private static void test2()
                {
                   System.out.println(“test2 Begin”);
                   int I = (int)i/0;    /*  ArithmaticException  */  
                   System.out.println(“test2 End”);
                }
             }                
javapadho
Exception when compiler recognized

Ducking :

Ducking is process where unhandled exception object propagate from place of rising and place of calling is called ducking. In previous program test2 () is rising and test1 () is calling.If handle unchecked exception than mean but try catch than program output.

javapadho
Exception when compiler recognized

Example 2:

 
                
             class ExplicityManager
             {
                public static void main(String []args)
                {
                   System.out.println(“Main Begin”);
                   test1();
                   System.out.println(“Main End”);
                }
                private static void test1()
                {
                   System.out.println(“test1 Begin”);
                   try
                   {
                      test2();
                   }
                   catch (ClassNotFoundException ex)
                   {
                      ex.printStackTrace();
                   }
                   System.out.println(“test1 End”);
                }
                private static void test2() throws ClassNotFoundException
                {
                   System.out.println(“test2 Begin”);
                   Class.forName("");  /*  CheckedTypeException So Must Be Throws  */
                   System.out.println(“test2 End”);
                }
             }                
javapadho
Exception when compiler recognized

Explicitly Ducking :

Explicitly is process done by using throws keyword checked type exception object propagate form rising place (test2) to calling place (test1) is called explicitly ducking.
Checked type exception solve by using throws key word. this key word throws the exception where calling method (test1) present.

Example 3:

 
                
             class ExplicityManager
             {
                public static void main(String []args) throws ClassNotFoundException
                {
                   System.out.println(“Main Begin”);
                   test1();    /*  Not Handled Check Type Exception  */
                   System.out.println(“Main End”);
                }
                private static void test1()throws ClassNotFoundException
                {
                   System.out.println(“test1 Begin”);
                          
                   test2();    /*  Not Handled Check Type Exception  */
                        
                   System.out.println(“test1 End”);
                }
                private static void test2() throws ClassNotFoundException
                {
                   System.out.println(“test2 Begin”);
                           
                   Class.forName(“”) /*  ExlicityDucking  */    
                   System.out.println(“test2 End”);
                }
             }                
javapadho
Exception when compiler recognized
  • Checked type exception: in this type exception auto ducking is not happed, developer develops explicitly ducking using throws.

  • Uncheck type exception not required, throws if you put compiler not give error.