When the sandbox is active (e.g. while debugging something), certains commands and expressions are not allowed (see :h sandbox) and may cause nasty exceptions to be thrown. So try to catch E48 and handle it gracefully
detect_spelllang
'flag'