Skip to content

Commit 8606daa

Browse files
committed
Catch exceptions and display in a dialog box
1 parent c8566b9 commit 8606daa

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

python/samples/i2cgui.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,17 @@ def addrbutton(s):
208208
self.SetSizerAndFit(vb)
209209
self.SetAutoLayout(True)
210210

211-
if len(self.devs) > 0:
211+
d1 = None
212+
while (d1 is None) and (len(self.devs) > 0):
212213
if preferred in self.devs:
213214
d1 = preferred
214215
else:
215216
d1 = min(self.devs)
216-
self.connect(self.devs[d1])
217+
try:
218+
self.connect(self.devs[d1])
219+
except serial.SerialException:
220+
del self.devs[d1]
221+
d1 = None
217222
cb.SetValue(d1)
218223

219224
t = threading.Thread(target=ping_thr, args=(self, ))
@@ -338,6 +343,13 @@ def hot(self, i, s):
338343

339344
if __name__ == '__main__':
340345
app = wx.App(0)
341-
f = Frame(*sys.argv[1:])
342-
f.Show(True)
343-
app.MainLoop()
346+
try:
347+
f = Frame(*sys.argv[1:])
348+
f.Show(True)
349+
app.MainLoop()
350+
except:
351+
import sys, traceback
352+
xc = traceback.format_exception(*sys.exc_info())
353+
dlg = wx.MessageDialog(None, "".join(xc), "i2cgui Error Trap", wx.OK | wx.ICON_WARNING)
354+
dlg.ShowModal()
355+
dlg.Destroy()

0 commit comments

Comments
 (0)