Rajdeep Biswas

Ranch Hand
+ Follow
since Mar 26, 2012
Merit badge: grant badges
For More
Bangalore
http://about.me/xploreraj
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Rajdeep Biswas

Hi,

I have a code that uses rx AsyncResponse as output and has below inside the method:


The thing is that the sequence of calls actually does not needs async but the way of handling errors seems like a good style to me. Without actually needing async model to rewrite this method, the simple idea will be to make the method calls and put all of it inside a try-catch block. But is there a better way to write such code? I'm using JDK 8.

Thanks
Rajdeep
1 year ago
Alright Dave. Checking and setting retries to 1 for wrong values was in my mind. Just thought if we have a better piece of code in the same manner as do ... (once), and while ...(retries) - try-again. It wont be an issue though, I will go with former only if thats that.
7 years ago
Ok, I am going to put things once again.

1. I am reading number of retries from outside.
2. Now, dont take it literally. If I read a value of 3 in retries, then, sat if we have the known exception in each execution, then total number of times the operation is executed is 3 (including original request, effectively meaning 2 retries instead of 3). Please ignore this thing. I will modify it later.
3. The way catch is defined and handled also is not in the scope of my discussion.

Here I am reiterating my issue:

1. If I do like this:

then I am getting issue that variable "retries" is not read. So to resolve this, I tried below:
2.
this now removes above problem but introduces another problem - redundant initialization of "retries", i.e. without being used, retries is being reassigned another value.
So I am looking for a clean approach to write it in a better way.
Here are my considerations:
  • The first call should be independent of whatever value we read for 'retries'
  • Duplicate code should be avoided, and avoiding recursion will be nice too.
  • 7 years ago
    That is not my problem at all. Please read the last statements in original post.
    7 years ago
    Considerations:
  • The first call should be independent of whatever value we read for 'retries'
  • Duplicate code should be avoided, and avoiding recursion will be nice too.

  • May be a simple thing, but I am not catching it probably. Please suggest.
    7 years ago
    As I said, it calls a boolean returning method passing on the exception object, and in the method we check for exception message contents and other things. But I wonder why should it matter because the assumption is that if the condition is met, i.e. if the method returns true, then we think of retry, anything else we throw the exception. My concerns I have put.

    7 years ago
    The instanceof is not there, just I added to make others understand. There is a method call that checks the contents of the exception.
    7 years ago
    Here's the logic I used:


    Number of retries is considering original operation as well. But the problem is that

    if I return from try block directly without assigning anything, then SCA (Fortify for me) reports that the variable retries is not read (in success flow), and
    if I assign and do as above, then SCA shouts about the immediate reassignment of value to the retries variable without even reading it.
    How do I write it nicely?

    7 years ago
    Welcome Mala. I hope readers and you both have great success with this book.

    Stephan van Hulst wrote:Understanding recursion lies in believing that the recursive step "just works".



    When I tried to solve it, thinking about individual movements confused me. Then I trusted recursion, thought about a single disk case calling that in recursion, and wrote the base condition, again just a print. It worked. When I go into it, it still utterly confuses me. So I am half in half out!    
    8 years ago
    For definition of exception, head to google.
    In regex, you simply match input for a given pattern without worrying for exceptions. Using parseInt or parseLong, you actually try to parse given string input to number (needed when you need given string input as a number in your application, since external inputs are always as String) and in case of error, gives exception that you should handle.
    In regex, you just match the input against a pattern without the worry of exceptions, since you are not actually parsing it to number.
    For example, head to http://regexr.com/3dmdj and mouseover the pattern.
    8 years ago
    Regex is also a way, but thing the OP mentioned, I think the simplest way will be to do Integer.parseInt(). If exception is thrown, its not a number. If not, its cool! Thinking this functionality will go in some utility method, just handle the NumberFormatException and return boolean appropriately.
    8 years ago
    Yup, please explain more. From the little understanding that I got, I think both threads are not doing inter-thread communication properly, or probably you needed to implement some polling mechanism against some state of your services. If you are knowing what you are trying to accomplish, BlockingQueue might be a good option too.
    8 years ago
    For anyone stumbling across this, here is the raw code; https://github.com/xploreraj/ytools/
    8 years ago
    JSP