How to raise a message box in case of not handled Error in Python?

Claudio

I have a huge in size Python script which takes much time for its execution, runs in background and is started in a way that I can't see its printing output.

In order to to be able to get notified when the script is finished I raise an appropriate message box, but if the script exits because of an Error I won't get notified.

Below a minimal code demonstrating what I am speaking about:

import math
try: 
    i = int('Oxff') 
except ValueError:
      ...             # some code handling the exception  
... ; pass ; ...      # some further code  
import no_such_module # in code not handled unexpected error 
... ; pass ; ...      # some further code  
# Message box notifying about successfull script run: 
from tkinter import Tk
root = Tk()
root.geometry('10x10+'+str(root.winfo_screenwidth()-10)+'+'+str(root.winfo_screenheight()-10))
from tkinter.messagebox import Message 
Message(title="Success", message="No Error", master=root).show()

So when my Python script fails due to an unexpected and therefore not in code handled error I will not get notified at all.

I don't want to enclose the entire script code with try: ... except: ... in order to handle unexpected and in code not handled errors.

Is there in Python a way to write a function which will be run displaying a message box in case an unexpected error terminates the script?

AKX

Swap in your own sys.excepthook to handle uncaught exceptions:

import sys
import traceback
from tkinter.messagebox import showerror


def oops(type, value, tb):
    showerror('Error', '\n'.join(traceback.format_exception(type, value, tb)))
    sys.exit(1)


sys.excepthook = oops


print(1 / 0)

pops up

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Raise custom error message for

How to Display python script Error in Tkinter message box

How do I use a Try and Exception to raise an error with a print message in Python?

How to get the error message in Controller or Route after handled exception in errorHandler?

How to raise Error message if any rows contains negative Values

How do I raise the same Exception with a custom message in Python?

How do I raise the same Exception with a custom message in Python?

How To Raise Exception with Message in Ada

Show message box in case of exception

How to raise an Error for the completionHandler?

How Virtual Box shared folder are handled in Debian ?

Fail Test Case if Exception is Raised and Handled in Python?

How do I get errors which are handled serverside in a ajax error message after submitting a form?

How to extract only error message from Oracle raise_application_error in C#

How is new object instantiation handled in case of Datasets?

How is orientation change handled in the message queue?

How to handle error raise - Asyncio in Python3

python, How can I put and if to a function that raise an error?

Code for waiting my previous condition for 2 minutes if not work , then raise an error message using python

Getting the error message "raise CalledProcessError(retcode, cmd)" when using the cat command from a python script

How can I log/print a custom error message when response.raise_for_status() == 404?

Django auth custom business logic - how do I raise custom error message?

How can I raise send a django error message in a class based view with a LoginRequiredMixin?

How can i raise a message error when introducing the same company and username

Python tkinter message box

How to display a changing message box on error and then continue code

CSS - how to place an error/hint message to the right of the input box

How to return a light box error message from a function

How to show Sqlite Triggers Raise message in toast