Page 1 of 1

How to catch an Error?

Posted: 26 Mar 2014 15:30
by bdeshi
Hey, how do I catch an arbitrary error?
For example, when hash() done on a folder which shows access denied error without any apparent return flags (is that the correct word?) .
ErrorCatching.png
yet another image, I love images!!! :kidding:

Re: How to catch an Error?

Posted: 26 Mar 2014 15:50
by TheQwerty
Instead of trying to catch the error you can ensure the item is a file with exists:

Code: Select all

$curitem = "<curitem>";
if (Exists($curitem) == 1) {
  msg hash(, $curitem, 3);
}
Another alternative is to use the {Dir ...} template of Report:

Code: Select all

$curitem = "<curitem>";
End $curitem == '', 'There is no current item.';
$curitem = Report('{Dir |{Fullname}|}', $curitem);
if ($curitem != '') {
  msg hash(, $curitem, 3);
}
However that's a waste for a single item and would be better for multiple items:

Code: Select all

// Show hash dialogs for each selected file.
$items = Report('{Dir |{Fullname}|}|', 1);
$items = FormatList($items, 'dents', '|');
foreach ($item, $items, '|') {
  if ($item != '') {
    msg $item . "<crlf>" . hash(, $item, 3);
  }
}

Re: How to catch an Error?

Posted: 26 Mar 2014 16:17
by SkyFrontier
My general rules for debugging include:

-check the punctuation/indents - they easily break the whole script or certain parts of it - easy to catch

-on while() loops, ensure that they'll have an end condition - specially when it comes to limiting after 'X' operations, being operation number <= total runs/number of tokens generally speaking; never forget to set a $op++ otherwise you'll get an endless loop - easy to be caught be the endless loop

-never loose the control structures inside a giant script - sometimes you add so much functionality to the point of losing the scope of each operation inside the whole scheme - not that easy to catch, unless you

--step;/unstep a lot - you NEED to see the script in action and mastering the points where to put then will save you precious minutes and headache

-be careful when using flags/permanent variables - flag variables can easily get lost inside many lines of code and interfere on a given point in an unexpected way; permanent variables may interfere from previous sessions, so it's wise to write unique permavars for each script

-don't forget that sub() requires extra step as it is seen as a script inside a script, not inheriting any step;'s from the main script

-also when it comes to sub(), don't forget to set global variables both on main script and sub() script so the required input/output variables are transported back and forth as intended
-still on while() loops, when the code looks perfect I'd start stepping right after a new loop is started - my hardest times debugging were spent by not knowing this tip, as here you'll probable have to check key variables so they have proper values - hard to catch

-exists() is your friend!

-comment the code specially at longer points, relevant to the code - blocks of subroutines, mainly, but small parts sometimes are prone to error and experience is the best guide here, as you'll know which errors YOU are likely to commit

-finally, read the error screens and search based on the output - sometimes the screen won't let you access involved variables yet you'll be able to see parts of the code, so you can step two or three commands before that point.

That said, I'd tell your 'access is denied!' in yellow means your <curitem> is currently being used by another process. Being under <xydata>, it's hard to guess as, despite several items loaded into memory, XY never gets a denial of access from Windows in my daily experience (that's portability is all about!).
I don't know how hash() reacts on a folder - no time to investigate now.

Re: How to catch an Error?

Posted: 26 Mar 2014 16:31
by bdeshi
@TheQWERTY Thanks! <EDIT> But I was specifically wondering if it was possible to catch these errors... <EDIT>

@SkyFrontier many thanks for the pointers! However, I created this error deliberately to illustrate my question. :lol: Also, this "Access Denied" isn't due to the item(s) being locked in another program, I've noticed hash() always returns "Access Denied" when its input is a folder. :veryconfused:

Actually if these type of errors could be caught in-script, error-handling will become easier. In this case I could determine if folders are among selected items ($selitems).

Re: How to catch an Error?

Posted: 13 Jun 2014 13:10
by bdeshi
Another plea to implement extended error catching/error returns: I'm writing a file into <xypath> to check for access rights to that dir. I already have code in place to handle denial/failure, but the access denied msg still shows up:

Code: Select all

...
 if (writefile("<xypath>\theme.tmp",,o) == 0){
  global $needsElevated =1; end1,,1;
 }
...
Everything's fine, except the glaring error message, plus it runs the risk of the user pressing "Cancel script".

Re: How to catch an Error?

Posted: 13 Jun 2014 21:31
by TheQwerty
You could use runret instead to attempt the write or to check the permissions with icacls.

Re: How to catch an Error?

Posted: 13 Jun 2014 21:35
by bdeshi
Right, good idea.