Given the following loop on each element of a list:
lists:foldl(fun(X) -> ... end,N,Y),
How to catch the errors and continue to loop on the elements ?
Same question if this code is in a gen_server and if process_flag(trap_exit, true) ?
Given the following loop on each element of a list:
lists:foldl(fun(X) -> ... end,N,Y),
How to catch the errors and continue to loop on the elements ?
Same question if this code is in a gen_server and if process_flag(trap_exit, true) ?
The 1st suggestion by @hdima is the easiest and gives you full control about how to handle different errors/throw etc. For example you may want to allow to throw as a form of non-local exit from the foldl. Are you really certain you want to ignore errors?
HandlerFactory though has an overly complex OO feel about it and seems a bit off. :-) It also severely limits your options.
This code will work within a gen_server
as its effect is purely local. Trapping exits will not affect this, or be affected by it, as exits are signals from other processes and these are not caught by try
. Turning on trap_exit
results in all exit signals from other processes being locally converted to messages which are accessed using receive
as any other message.
Why you just can't use try/catch like this?
Or you can use a decorator function if you want to extract error handling, like this: