# Method for calculation Cauchy Inequality with fully implemented handler

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-11-2012, 03:52 PM
iliyapolak
Method for calculation Cauchy Inequality with fully implemented handler
Hello guys!
I would like to post my method for calculating Cauchy Inequality.Inside catch clause i implemented do-while loop as a handler to handle null values passed to a method.Need your opinion on handler implementation.

Thank you.
Code:

``` public static double cauchyInequality(double value,double value2) throws IllegalArgumentException , ArithmeticException  {                             double a = value;               double b = value2;                             double left_term;               double right_term = 1;               double num1=a;               double num2=b;               double left_sum = 1;               double right_product = 1;               double left_product = 1;               double difference;                             try {                                     if (num1 == 0 || num1 <-1)                       throw new IllegalArgumentException("first parameter must be greater than 0");               else if (num2 == 0 || num2 <-1)                   throw new ArithmeticException("second parameter can not be 0");                         }catch (IllegalArgumentException e)    {                   e.printStackTrace(System.out);                   do {  // handler implementation starts here                                                 ++num1;                         ++num2;                                                 left_sum+= num1*num2;                         left_term = Math.pow(left_sum, 2.0);                                                 left_product+= Math.pow(num1, 2.0);                         right_product+=Math.pow(num2, 2.0);                         right_term = left_product*right_product;                                                 difference = left_term-right_term;                                                 System.out.println("num1 " + num1 + "num2 " + num2 + "left_sum " + left_sum + " difference " + difference + "right term  "+ right_term + " left term " + left_term);                                             } while (left_term<10000 && right_term<10000);                                                             for(StackTraceElement element : e.getStackTrace()){                     System.out.println(  element.getLineNumber()+ " " + element.getMethodName() + " " + element.getClassName() + " ");                   }               } catch(ArithmeticException e) {                   e.printStackTrace(System.out);                                                     for (StackTraceElement element : e.getStackTrace()){                       System.out.println(element.getLineNumber() + " " + element.getMethodName()+ " " + element.getClassName() + " ");                   }               }                               do {                                                 ++num1;                         ++num2;                                                 left_sum+= num1*num2;                         left_term = Math.pow(left_sum, 2.0);                                                 left_product+= Math.pow(num1, 2.0);                         right_product+=Math.pow(num2, 2.0);                         right_term = left_product*right_product;                                                 difference = left_term-right_term;                                                 System.out.println("num1 " + num1 + "num2 " + num2 + "left_sum " + left_sum + " difference " + difference + "right term  "+ right_term + " left term " + left_term);                                             } while (left_term<10000 && right_term<10000);                         return difference;           }```
• 01-11-2012, 04:06 PM
Tolls
Re: Method for calculation Cauchy Inequality with fully implemented handler
WHy are you doing processing inside the catch block like that?
That sort of implies it's not an exception.
You shouldn't use exception handling as a means of normal flow control.
• 01-11-2012, 04:31 PM
iliyapolak
Re: Method for calculation Cauchy Inequality with fully implemented handler
Thank You for your answer.Processing inside catch block is triggered only when one or both of the arguments are 0.
When I enter values greater than 0 exception is not triggered and normal do-while loop is processed.
My intention was to implement exception handling inside catch clause in order to reenter do - while loop with default values(not faulty 0's).
Simply i tried inside catch clause to" repair the code" instead of throwing an exception.
• 01-11-2012, 05:54 PM
Tolls
Re: Method for calculation Cauchy Inequality with fully implemented handler
Why use exceptions for that?
Just stick that code inside the relevant parts of the if statements which are throwing the exceptions.

This is not what exceptions are for.
If you have a way of repairing the code, then it's not an exception, especially since this is within the method throwing the exception.
• 01-11-2012, 06:25 PM
iliyapolak
Re: Method for calculation Cauchy Inequality with fully implemented handler
Yes i could "repair" faulty conditions in if-statements , but i was inspired by Bruce Eckel book " Thinking in Java 4th edition" where he wrote about the correcting code inside catch clause and implementing exception handler which
can do more than only displaying stack.It was only an experiment:)-:
• 01-11-2012, 06:31 PM
Tolls
Re: Method for calculation Cauchy Inequality with fully implemented handler
Oh right.
If it's just an experiment, then OK.
• 01-11-2012, 07:09 PM
iliyapolak
Re: Method for calculation Cauchy Inequality with fully implemented handler
After writing another Inequality method and experimenting with code inside catch clause method's flow control inside catch block entered some kind of loop throwing exceptions but continued execution until i changed while statement terminating condition.
You were right catch block is not for flow redirecting and code correction.
• 01-11-2012, 07:20 PM
Tolls
Re: Method for calculation Cauchy Inequality with fully implemented handler
I don't know what Bruce says in that edition, but I suspect his idea of correction was a call to a method that throws an illegal argument, and then using the catch to try and correct one of the parameters?
• 01-11-2012, 08:01 PM
iliyapolak
Re: Method for calculation Cauchy Inequality with fully implemented handler
He was talking about resumption vs termination his idea was when using resumption place try block inside a while loop that keeps reentering the try block until the result is satisfactionary or simply do not throw a exception and instead call a method that fixes the situation.
• 01-12-2012, 05:51 PM
iliyapolak
Re: Method for calculation Cauchy Inequality with fully implemented handler
Finally tried resumption with outer while loop and try block inside that loop works fine.
• 01-15-2012, 01:37 PM
AndreB
Re: Method for calculation Cauchy Inequality with fully implemented handler
yes... a really deep interpretation! (not!)

if you want to make your code less efficient you sure will code you algorithm throwing and catching exceptions! This is the most efficient way to slow down the whole thing. (Use an additional synchronize block in your loop, for additional speed break!)

The concept of exeptions is not meant for normal code flow ... and not even Bruce (Almighty) could have suggested it.
You should use exceptions for "known uknowns" (Unknown unknowns and the Rest)
There you can try and "fix" your "unknown situtation" with a different code
• 01-16-2012, 06:57 PM
iliyapolak
Re: Method for calculation Cauchy Inequality with fully implemented handler
AndreB
It was only an experiment as i stated earlier.Later i used try block with while loop checking constantly for wrong values entered and this works great.I also could have used if-statements with return 0 or handle the wrong values with function call inside catch block(callee(method for calculation Cauchy inequality )returns unexpected result of calculation).
• 01-16-2012, 07:21 PM
AndreB
Re: Method for calculation Cauchy Inequality with fully implemented handler

Code:

```public static double cauchyInequality(double value,double value2) {                         double a = value;         double b = value2;                 double left_term;         double right_term = 1;         double num1=a;         double num2=b;         double left_sum = 1;         double right_product = 1;         double left_product = 1;                 // default value is not a number         double difference = Double.NaN;                 if (num1 == 0 || num1 <-1) {                 // handle wrong inputs here         } else if (num2 == 0 || num2 <-1) {                 // handle wrong inputs here         } else {                 do {  // handler implementation starts here                                 ++num1;                 ++num2;                               left_sum+= num1*num2;               left_term = Math.pow(left_sum, 2.0);                               left_product+= Math.pow(num1, 2.0);               right_product+=Math.pow(num2, 2.0);               right_term = left_product*right_product;                               difference = left_term-right_term;                               System.out.println("num1 " + num1 + "num2 " + num2 + "left_sum " + left_sum + " difference " + difference + "right term  "+ right_term + " left term " + left_term);                             } while (left_term<10000 && right_term<10000);         }                       return difference;     }```
• 01-16-2012, 07:57 PM
iliyapolak
Re: Method for calculation Cauchy Inequality with fully implemented handler
• 01-16-2012, 08:23 PM
AndreB
Re: Method for calculation Cauchy Inequality with fully implemented handler
i'm not against using exceptions.
exceptions are a very useful concept in the right place.

using exceptions for code flow is "bad" programming practice

- exceptions make your code slow, exception handling is a very costly operation
- you don't throw exceptions only to handle them in the same call procedure (you usally catch exceptions which come from inner functions and you throw exceptions to hint that a parent method should handle them)
- exceptions are meant to control the "unexpected" (like file handling for example: if you read a file an IOExceptions may occur if you hard disk crashes - that was unexpected!). the rule of thumb here is: exception are a concept to handle unexpected problems (known unknowns) - using exceptions to check for the input for a function is wrong, because you can expect some wrong input and you can handle it with some input check method
- your code becomes very hard to read if you put exceptions for code flow

and there are many other major and minor reasons...
• 01-16-2012, 08:52 PM
iliyapolak
Re: Method for calculation Cauchy Inequality with fully implemented handler
And what do you think about this code should i only throw an exceptions and do not handle them in the same method or maybe use only if-statement with return 0.

Code:

``` public static double limitSin(double value) throws IllegalArgumentException ,ArithmeticException{                                                     double sine;                           double x = value;                           double limit = 0;                                                                               try {                                                                     if (value <0 || value >Math.PI/2)                                           throw new IllegalArgumentException("argument x must be between 0 and Pi/2 \t" + value);                                                             }catch(IllegalArgumentException e){                                   ExceptionLogger(e);                                                                     for(StackTraceElement element : e.getStackTrace()){                                           System.out.println(element.getLineNumber()+ " " + element.getMethodName()+ " " + element.getClassName() + " ");                                   }                           }                                                               try {                                                                                         for(x = value ; x<Math.PI/2;x+=0.001){                                                                                                         if (value <0 || value > Math.PI/2)                                                             throw new ArithmeticException("argument x must be between x and Pi/2 \t "  +"value passed is \t " + value);                                                                                                         sine = Math.sin(x);                                                     limit = sine/x;                                                                                                         System.out.printf("%15s %32s %30s %30s\n" ,"sin("+x+")" , "rad("+x+")" , "limit" , "arg x");                                                     System.out.printf("%.16f %35.16f %35.16f % 25.3f\n" , sine , x , limit , x);                                             }                                     }catch(ArithmeticException e){                                             ExceptionLogger(e);                                                                                         for(StackTraceElement element : e.getStackTrace()){                                                     System.out.println(element.getLineNumber()+ " " + element.getMethodName()+ " " + element.getClassName() + " ");                                             }                                     }                                                                         return limit;```
• 01-16-2012, 09:16 PM
AndreB
Re: Method for calculation Cauchy Inequality with fully implemented handler
Well...

there are two ways of solving this:
i would prefer checking the input values in the caller object before even calling limitSin method

then your method(s) would look like

Code:

```        public void callerMethod() {                 double value = 0f;                 //... get input and check it                 if (value <0 || value >Math.PI/2) {                         System.out.println("argument x must be between 0 and Pi/2 \t");                 } else {                         double result = limitSin(value);                         if (result == Double.NaN) {                                 // something was wrong with the calculation                         }                 }         }```
and the function itself would be

Code:

```public static double limitSin(double value) throws IllegalArgumentException ,ArithmeticException{                 double sine;                 double x = value;                 double limit = Double.NaN;                 for(x = value ; x<Math.PI/2;x+=0.001){                         sine = Math.sin(x);                         limit = sine/x;                         System.out.printf("%15s %32s %30s %30s\n" ,"sin("+x+")" , "rad("+x+")" , "limit" , "arg x");                         System.out.printf("%.16f %35.16f %35.16f % 25.3f\n" , sine , x , limit , x);                 }                                 return limit;         }```
the other way is what you suggested... returning 0. But if zero value is also a valid value you should propably return Double.NaN

Code:

```public static double limitSin(double value) throws IllegalArgumentException ,ArithmeticException{                 double sine;                 double x = value;                 double limit = 0;                 if (value <0 || value >Math.PI/2) {                         return Double.NaN;                 } else {                         for(x = value ; x<Math.PI/2;x+=0.001){                                 // this can actually never happen because you are already in the else branch                                 // thus the value is already 0 >= value < Math.PI/2                                 // the value also cannot exceed Math.PI/2 because the for loop would exit                                 // when x >= Math.PI/2                                 //if (value <0 || value > Math.PI/2)                                 //    throw new ArithmeticException("argument x must be between x and Pi/2 \t "  +"value passed is \t " + value);                                 sine = Math.sin(x);                                 limit = sine/x;                                 System.out.printf("%15s %32s %30s %30s\n" ,"sin("+x+")" , "rad("+x+")" , "limit" , "arg x");                                 System.out.printf("%.16f %35.16f %35.16f % 25.3f\n" , sine , x , limit , x);                         }                 }                 return limit;         }```
an even shorter way would be:

Code:

```public static double limitSin(double value) throws IllegalArgumentException ,ArithmeticException{                 double sine;                 double x = value;                 double limit = Double.NaN;                 if (value >=0) {                         for(x = value ; x<Math.PI/2;x+=0.001){                                 sine = Math.sin(x);                                 limit = sine/x;                                 System.out.printf("%15s %32s %30s %30s\n" ,"sin("+x+")" , "rad("+x+")" , "limit" , "arg x");                                 System.out.printf("%.16f %35.16f %35.16f % 25.3f\n" , sine , x , limit , x);                         }                 }                 return limit;         }```
• 01-16-2012, 09:57 PM
iliyapolak
Re: Method for calculation Cauchy Inequality with fully implemented handler
Thanks for your helping hand. I think that i still must pay attention to those subtle details.In my earlier code i only used throw statements and all error handling was done by the callers , but as i'm still learning java i tried to implement exceptions in my code.
By the way in the code above i did not handle division by zero , but it is unchecked exception and should be handled in the run-time.
• 01-17-2012, 10:44 AM
Tolls
Re: Method for calculation Cauchy Inequality with fully implemented handler
Disagree.
iliyapolak's example above is a classic example of where exceptions should be applied.

It's a static helper method.
Having the calling method's check that the parameters they are supplying are correct (and therefore having the method assume the parameters are correct) strikes me as slightly optimistic.
Returning 0 as an error signal is a definite code smell as it requires the calling code to interpret that value as an error.

This should throw an IllegalArgument (or similar), though not quite as written in iliya's code.
• 01-17-2012, 11:00 AM
AndreB
Re: Method for calculation Cauchy Inequality with fully implemented handler
Quote:

Originally Posted by Tolls
Disagree.
iliyapolak's example above is a classic example of where exceptions should be applied.

It's a static helper method.
Having the calling method's check that the parameters they are supplying are correct (and therefore having the method assume the parameters are correct) strikes me as slightly optimistic.

What makes you think so? Why should execeptions be used in this particular example?

To my best knowledge defensive programming (Defensive programming - Wikipedia, the free encyclopedia) is a good programming practice! I don't see any optimism here ;-)

Quote:

Originally Posted by Tolls
Returning 0 as an error signal is a definite code smell as it requires the calling code to interpret that value as an error.

If you look closely into the code, i do not return a "0" value. In the examples above I use Double.NaN as return value for calculations which is actually also a standard behaviour for bad calculations in java.

It would be nice, if you would provide an example of how you would use exceptions in that particular example.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last