Exception handling for main thread