Added exception handling for improved debugging.