add exception handling.

add exception handling, instead of fprintf.