errorPtr optional; if NULL then throw on error