Skip to content

Commit c6d1169

Browse files
Improve exception handling with specific exception types
Co-authored-by: codingwithnsh <138281862+codingwithnsh@users.noreply.github.com>
1 parent bb43253 commit c6d1169

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

mac_ui_enhancements.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ def preview(self, filepath):
358358
text_widget.pack(fill=tk.BOTH, expand=True)
359359

360360
try:
361-
with open(filepath, 'r') as f:
361+
with open(filepath, 'r', encoding='utf-8') as f:
362362
text_widget.insert('1.0', f.read())
363363
text_widget.config(state='disabled')
364-
except:
365-
text_widget.insert('1.0', "Cannot preview this file")
364+
except (UnicodeDecodeError, FileNotFoundError, PermissionError, OSError) as e:
365+
text_widget.insert('1.0', f"Cannot preview this file: {str(e)}")
366366

367367
elif ext in ['.png', '.jpg', '.jpeg', '.gif', '.bmp']:
368368
# Image preview
@@ -375,8 +375,8 @@ def preview(self, filepath):
375375
label = tk.Label(content, image=photo, bg='#1a1a1a')
376376
label.image = photo # Keep reference
377377
label.pack(expand=True)
378-
except:
379-
tk.Label(content, text="Cannot preview image", bg='#1a1a1a',
378+
except (ImportError, IOError, OSError) as e:
379+
tk.Label(content, text=f"Cannot preview image: {str(e)}", bg='#1a1a1a',
380380
fg='white', font=('Arial', 14)).pack(expand=True)
381381

382382
else:

sandbox_manager.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def create(self):
6363
self.save_config()
6464

6565
return True
66-
except Exception as e:
66+
except (OSError, PermissionError) as e:
6767
print(f"Error creating sandbox: {e}")
6868
return False
6969

@@ -92,10 +92,10 @@ def stop(self):
9292
try:
9393
proc.terminate()
9494
proc.wait(timeout=3)
95-
except:
95+
except (subprocess.TimeoutExpired, ProcessLookupError):
9696
try:
9797
proc.kill()
98-
except:
98+
except (ProcessLookupError, PermissionError):
9999
pass
100100
finally:
101101
if proc in self.processes:
@@ -118,7 +118,7 @@ def pause(self):
118118
for proc in self.processes:
119119
try:
120120
proc.suspend()
121-
except:
121+
except (AttributeError, ProcessLookupError, PermissionError):
122122
pass
123123

124124
self.status = "paused"
@@ -138,7 +138,7 @@ def resume(self):
138138
for proc in self.processes:
139139
try:
140140
proc.resume()
141-
except:
141+
except (AttributeError, ProcessLookupError, PermissionError):
142142
pass
143143

144144
self.status = "running"
@@ -210,7 +210,7 @@ def update_stats(self):
210210
p = psutil.Process(proc.pid)
211211
total_cpu += p.cpu_percent(interval=0.1)
212212
total_memory += p.memory_info().rss / (1024 * 1024) # MB
213-
except:
213+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
214214
if proc in self.processes:
215215
self.processes.remove(proc)
216216

0 commit comments

Comments
 (0)