Exceptions and .pyw

Hi,
  I'm pretty new with wxWindows. If I use a .pyw for an application without a console, but when an exception is raised, I would want a pop-up with the exception content. What is a simple way to do that? I would want to use that pattern for my small applications.

Thx and Regards,

Nicolas

  I'm pretty new with wxWindows. If I use a .pyw for an application
without a console, but when an exception is raised, I would want a
pop-up with the exception content. What is a simple way to do that? I
would want to use that pattern for my small applications.

Where you create your app, pass True as an argument, i.e.

app = MyApp(True)

... will do a popup window that shows all output to stdio / stderr, and

app = MyApp(False)

... will do no popup.

Jeff Grimmett wrote:

Where you create your app, pass True as an argument, i.e.

app = MyApp(True)

... will do a popup window that shows all output to stdio / stderr, and

Thx, I just did that and I have the pop-up. However, it disappears immediatly if the exception occurs during MyApp.OnInit. It there's a way to keep it even in that case?

Thx and Regards,

Nicolas

Thx, I just did that and I have the pop-up. However, it disappears
immediatly if the exception occurs during MyApp.OnInit. It there's a
way to keep it even in that case?

Ooo, black magic voodoo kinda stuff. MAYBE you could use wx.lib.ErrorDialogs
to get around this, but I'm dubious about it.

Nicolas Fleury wrote:

Jeff Grimmett wrote:

Where you create your app, pass True as an argument, i.e.

app = MyApp(True)

... will do a popup window that shows all output to stdio / stderr, and

Thx, I just did that and I have the pop-up. However, it disappears immediatly if the exception occurs during MyApp.OnInit. It there's a way to keep it even in that case?

Somebody posted a neat way to do it on this list before, but I can't find it now... Usually I just add try/except clauses in OnInit and take care of things there.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Maybe this will work for you.

I use this code to capture the errors to a file. This code is from the
demo code.

Put this in your main:

if __name__ == '__main__':

···

#
    # Redirect python exceptions to a file
    #
    sys.stderr = ExceptionHandler()

and you'll be all set.

================================================================

#
# Dutifully re-used from demo code
#
class ExceptionHandler:
    """ A simple error-handling class to write exceptions to a text file.

  Under MS Windows, the standard DOS console window doesn't scroll and
  closes as soon as the application exits, making it hard to find and
  view Python exceptions. This utility class allows you to handle Python
  exceptions in a more friendly manner.
    """

    def __init__(self):
  """ Standard constructor.
  """
  self._buff = ""
  if os.path.exists("errors.txt"):
      os.remove("errors.txt") # Delete previous error log, if any.

    def write(self, s):
  """ Write the given error message to a text file.

      Note that if the error message doesn't end in a carriage return, we
      have to buffer up the inputs until a carriage return is received.
  """
  if (s[-1] != "\n") and (s[-1] != "\r"):
      self._buff = self._buff + s
      return

  try:
      s = self._buff + s
      self._buff = ""

      if s[:9] == "Traceback":
    # Tell the user than an exception occurred.
    wxMessageBox("An internal error has occurred.\nPlease " + \
           "refer to the 'errors.txt' file for details.",
           "Error", wxOK | wxCENTRE | wxICON_EXCLAMATION)

      f = open("errors.txt", "a")
      f.write(s)
      f.close()
  except:
      pass # Don't recursively crash on errors.

On Fri, 2004-01-09 at 21:39, Jeff Grimmett wrote:

> Thx, I just did that and I have the pop-up. However, it disappears
> immediatly if the exception occurs during MyApp.OnInit. It there's a
> way to keep it even in that case?

Ooo, black magic voodoo kinda stuff. MAYBE you could use wx.lib.ErrorDialogs
to get around this, but I'm dubious about it.

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

--
Jim West
CheckLogix -a Concord company
102 S. Tejon Ste 920, Colorado Springs, CO 80903
719.633.7005 x223 Fax: 719.633.7006 Cell: 719.660.5676

Robin Dunn wrote:

Nicolas Fleury wrote:

Thx, I just did that and I have the pop-up. However, it disappears immediatly if the exception occurs during MyApp.OnInit. It there's a way to keep it even in that case?

Somebody posted a neat way to do it on this list before, but I can't find it now... Usually I just add try/except clauses in OnInit and take care of things there.

I'm really interested in that neat way, but I haven't been able to find anything either with my searches. Do you know what is the dialog used to display exceptions, so that I can use it if I handle myself stuff in OnInit? Would make that dialog modal, if there's any way, solve the problem?

Thx and regards,

Nicolas

Nicolas Fleury wrote:

Robin Dunn wrote:

Nicolas Fleury wrote:

Thx, I just did that and I have the pop-up. However, it disappears immediatly if the exception occurs during MyApp.OnInit. It there's a way to keep it even in that case?

Somebody posted a neat way to do it on this list before, but I can't find it now... Usually I just add try/except clauses in OnInit and take care of things there.

I'm really interested in that neat way, but I haven't been able to find anything either with my searches. Do you know what is the dialog used to display exceptions, so that I can use it if I handle myself stuff in OnInit? Would make that dialog modal, if there's any way, solve the problem?

I still don't remember what the other way was, but this works:

class MyApp(wx.App):
     def OnInit(self):
         try:
             frame = MyFrame(None, "Simple wxPython App")
             frame.Show(True)
         except:
             import traceback
             traceback.print_exc()
             dlg = wx.MessageDialog(
                None, "An exception occurred in OnInit!\n"
                "Please report the error shown in in the output window.",
                "Error!", wx.OK | wx.ICON_EXCLAMATION)
             dlg.ShowModal()
      dlg.Destroy()
         return True

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

I still don't remember what the other way was, but this works:

Maybe it would be nice to have that the parameter of wxApp used to specify that kind of mechanism by using more than a boolean:
False => wxDEFAULT_CONSOLE=0
True => wxCONSOLE_WINDOW=1
wxMODAL_CONSOLE_WINDOW=2

The names may not be appropriate, but it's enough to get the idea and it would be backward-compatible. What do you think?

Regards,

Nicolas

Nicolas Fleury wrote:

Robin Dunn wrote:

I still don't remember what the other way was, but this works:

Maybe it would be nice to have that the parameter of wxApp used to specify that kind of mechanism by using more than a boolean:
False => wxDEFAULT_CONSOLE=0
True => wxCONSOLE_WINDOW=1
wxMODAL_CONSOLE_WINDOW=2

The names may not be appropriate, but it's enough to get the idea and it would be backward-compatible. What do you think?

I think that would complicate things too much because for 95% of the things that the the output window is used for now it works just fine. However there is an easy way for you to extend it in your own apps, just set the App.outputWindowClass member to be the class you want it to use. For example:

class MyApp(wx.App):
  outputWindowClass = MyOutputWindowClass
  def OnInit(self):
    ...

The default is PyOnDemandOutputWindow.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!