2021.11.10 12:17 PM
closeHandle:{[h]
.util.logMsg["Closing handle"];
update handle:0Ni from `.cfg.handles where handle=h;
@[hclose; h;.util.logError["Error closing handle: ",(string h)]];
};
logMsg:{[x]
show string[.z.Z], "-" , x;
};
logError:{
logMsg["ERROR: ",x];
:`error;
};
For some reason the trap is printing error closing handle when infact its closing it. I only want it to print when it might have issue closing it. Am i missing something?
2021.11.10 09:29 PM - edited 2021.11.10 09:31 PM
The error thrown is passed as the first parameter to the function. https://code.kx.com/q/ref/apply/#trap. To allow parameters, you need to have a function of rank 2 or greater, you can then create a projection of a rank 1 function by eliding the first parameter:
q)logError:{-1"Error: ",x;}
q)@[10+;`abc;{logError x,". Couldn't add 10 to ",string y}[;`abc]]
Error: type. Couldn't add 10 to abc
You can extend this to allow additional parameters to your error function by creating greater rank functions
2021.11.10 02:08 PM
Hi, the trap itself needs to be a function otherwise it will be evaluated regardless of whether the application of your function (hclose) fails or not.
https://code.kx.com/q/ref/apply/#when-e-is-not-a-function
q)logError:{-1"Error: ",x;}
q)@[10+;10;logError"Can't add"]
Error: Can't add
20
q)@[10+;10;{logError"Can't add"}]
20
q)@[10+;`abc;{logError"Can't add"}]
Error: Can't add
2021.11.10 09:13 PM
This looks good but how can i pass a parameter to that function? doesnt seem to allow me to pass anything.
2021.11.10 09:29 PM - edited 2021.11.10 09:31 PM
The error thrown is passed as the first parameter to the function. https://code.kx.com/q/ref/apply/#trap. To allow parameters, you need to have a function of rank 2 or greater, you can then create a projection of a rank 1 function by eliding the first parameter:
q)logError:{-1"Error: ",x;}
q)@[10+;`abc;{logError x,". Couldn't add 10 to ",string y}[;`abc]]
Error: type. Couldn't add 10 to abc
You can extend this to allow additional parameters to your error function by creating greater rank functions
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.