fix exception handling, add null check.