0

I want to generate a file through randomly selecting an index from a list. That works. However, I occasionally get the error: ERR_FILE_NOT_FOUND.

I tried using the try.. catch statement but that doesn't quite achieve what I would like. I'm not familiar with JavaScript, but I know in python I could do something like:

while True:
    # generate the image
    try:
        # do stuff
    except Exception:
        # do nothing so it regenerates an image
    else:
        break

This doesn't work in JavaScript, the "else" says it expects a decision.

xupaii
  • 465
  • 4
  • 15
  • 31
  • So basically just keep trying to generate an image until it succeeds? – Carcigenicate Jul 28 '19 at 17:58
  • Yeah, basically – xupaii Jul 28 '19 at 18:01
  • This gives some related suggestions: https://stackoverflow.com/questions/4872170/javascript-try-catch-else-finally-like-python-java-ruby-etc (and of course if you really want to keep repeating code forever until it succeeds -- or crashes --, you would use a `while` loop, which JavaScript has) – Cat Jul 28 '19 at 18:17
  • What are you planning to do when the error happens? `ERR_FILE_NOT_FOUND` is a browser error, so you cannot `catch` it with Javascript. – Magnum Jul 28 '19 at 18:33
  • Hey in which case you want to break the loop. – Krishna Kamal Jul 28 '19 at 18:07
  • Then I don't have a clue, to be honest – xupaii Jul 28 '19 at 19:39
  • Not sure what API you are working with for the files, but if it is asynchronous then you should check out promises - they [provide a way to distinguish `except` and `else` cases](https://stackoverflow.com/a/24663315/1048572) with a dedicated method. See also [here](https://stackoverflow.com/a/44664037/1048572). – Bergi Jul 28 '19 at 21:02

0 Answers0