throwFileError

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

xbytor

throwFileError

Post by xbytor »

Here's a handy little utility function from stdlib.js.

Code: Select allthrowFileError = function(f, msg) {
  if (msg == undefined) msg = '';
  throw IOError(msg + '\"' + f + "\": " + f.error + '.');
};


What good is this? Well, it let's you do stuff like this:

Code: Select allfile.copy(otherFile) || throwFileError(file, "Copy failed");

Most of the File IO functions return true if everything is OK and false if there was a problem. So, if the file.copy call succeeds, the || expressions short-circuits and doesn't evaluate the throwFileError call. If the copy fails, on the other hand, the throwFileError function is called and an exception is throw with (hopefully) useful diagnostic text.

It's useful for situations where throwing an exception is a suitable response to an IO error. Depending on the situation, sometimes I use this technique, sometimes I check the result and handle failures in a more context-sensitive fashion, and other times I don't do anything

-X