Copy link to clipboard
Copied
I have the following catch block in the outermost function in a script.
} catch(err) { | ||
restoreOriginalSettings (); | ||
throw err; | ||
} |
When one of my testers runs the script he gets "Error 54: Uncaught Javascript exception: No such element. Line 399 throw err." where line 399 is the throw err statement. I assume "No such element" means that the exception object err does not exist. How can I be in a catch without an exception object? I must be missing something but I don't know what. I suppose I can wrap the throw err statement in a try/catch and swallow the exception but that does not feel right. Any suggestions? Thanks.
Copy link to clipboard
Copied
What’s
throw
in the context of JavaScript?
Copy link to clipboard
Copied
throw raises an exception. for example:
throw new Error("My custom exception");
creates a new exception object with the message "My custom exception" and raises or throws the exception. throw combined with try/catch is the way to create and handle exceptions in javascript. In my original post I was attempting to re-throw the exception caught by the catch.
Copy link to clipboard
Copied
Interesting. The bogus exception must be coming from one of the Adobe API functions because all of the exceptions I throw in my code are of the form
throw new Error("Error message");
Copy link to clipboard
Copied
In Javascript you can "throw anything", i.e. number, string, object, etc. Might be you are allowed to throw undefined exceptions, i.e. just "throw;". Then when you try to use "err" you'd get the error you described.
Copy link to clipboard
Copied
Thanks.
Bazsl, have you tried something other than "throw" yet, "alert" for example?
Copy link to clipboard
Copied
No because I can't reproduce the error on my machine. A friend who is testing the script gets it. IAC, assuming Matias is correct that something in Adobe's code is throwing something other than an exception object the best solution seems to be:
catch(err) {
try {
throw err;
catch(e) {}
}
since I don't know what to do with something that is not an exception.