Fix exception handling and use "" for NULL

Fixes #1546, fixes #1547