I have a (declarative) line in my QML file which is logging an error and I want to catch it and log certain variables to try and figure out what's going on. The line is something like (contained in a Repeater
hence the use of index
):
a: ((n === -1) || (n - p > 7) || (index >= t)) ? "" : b[p+index].c
(no, those aren't my actual variable names, I've renamed them to prevent information leakage - the actual names are not important).
When running the code, I am occasionally getting the error:
file:///path/to/SomeFile.qml:7: TypeError: Cannot read property 'c' of undefined
leading me to believe that one of those variables is wrong somehow when the a
field is being modified based on the other variables.
I am aware of try/catch
in procudural QML code but I'm not sure how to do something similar for declarative code (or even if it's possible).
Is there a way to catch that error and print out all the relevant variables that exist at the time the error occurs?
Maybe I'm not understanding the question, but the a:
assignment can be the result of a function or even just a block of JS code which returns some value. So you can freely use try/catch or anything else for that matter.
a: {
try {
return ((n === -1) || (n - p > 7) || (index >= t)) ? "" : b[p+index].c;
}
catch(e) {
console.log(e);
return "FUBAR";
}
}
ADDED: The return
keywords are actually optional here, works just as well w/out them.
Also to point out:
a: ((n === -1) || (n - p > 7) || (index >= t)) ? "" : b[p+index].c;
is same as:
a: { ((n === -1) || (n - p > 7) || (index >= t)) ? "" : b[p+index].c; }
The curly braces are simply optional for single-line expressions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With