For example, when hash() done on a folder which shows access denied error without any apparent return flags (is that the correct word?) . yet another image, I love images!!!
How to catch an Error?
-
bdeshi
- Posts: 4256
- Joined: 12 Mar 2014 17:27
- Location: Asteroid B-612
- Contact:
How to catch an Error?
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?) . yet another image, I love images!!!
For example, when hash() done on a folder which shows access denied error without any apparent return flags (is that the correct word?) . yet another image, I love images!!!
To see the attached files, you need to log into the forum.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
TheQwerty
- Posts: 4373
- Joined: 03 Aug 2007 22:30
Re: How to catch an Error?
Instead of trying to catch the error you can ensure the item is a file with exists:Another alternative is to use the {Dir ...} template of Report:However that's a waste for a single item and would be better for multiple items:
Code: Select all
$curitem = "<curitem>";
if (Exists($curitem) == 1) {
msg hash(, $curitem, 3);
}Code: Select all
$curitem = "<curitem>";
End $curitem == '', 'There is no current item.';
$curitem = Report('{Dir |{Fullname}|}', $curitem);
if ($curitem != '') {
msg hash(, $curitem, 3);
}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);
}
}-
SkyFrontier
- Posts: 2341
- Joined: 04 Jan 2010 14:27
- Location: Pasárgada (eu vou!)
Re: How to catch an Error?
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.
-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.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...
-
bdeshi
- Posts: 4256
- Joined: 12 Mar 2014 17:27
- Location: Asteroid B-612
- Contact:
Re: How to catch an Error?
@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.
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.
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).
@SkyFrontier many thanks for the pointers! However, I created this error deliberately to illustrate my question.
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).
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
bdeshi
- Posts: 4256
- Joined: 12 Mar 2014 17:27
- Location: Asteroid B-612
- Contact:
Re: How to catch an Error?
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:
Everything's fine, except the glaring error message, plus it runs the risk of the user pressing "Cancel script".
Code: Select all
...
if (writefile("<xypath>\theme.tmp",,o) == 0){
global $needsElevated =1; end1,,1;
}
...Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
TheQwerty
- Posts: 4373
- Joined: 03 Aug 2007 22:30
Re: How to catch an Error?
You could use runret instead to attempt the write or to check the permissions with icacls.
-
bdeshi
- Posts: 4256
- Joined: 12 Mar 2014 17:27
- Location: Asteroid B-612
- Contact:
Re: How to catch an Error?
Right, good idea.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
XYplorer Beta Club