Iram shahzadi
Iram shahzadi

Reputation: 43

Using beep sound at point of error in python

I want to add beep sound when an error occurs in python script. I know how to add windows beep after specific line e.g.

 duration = 1000  # milliseconds
    freq = 440  # Hz
     #some code here
    winsound.Beep(freq, duration)

Is it possible to enable beep whenever there is an error? I am using windows 10, python 3.6, and pycharm IDE. I couldn't find any feature in pycharm that gives audio notification on error.

Upvotes: 2

Views: 2174

Answers (3)

dasWesen
dasWesen

Reputation: 617

PyCharm-specific: By now, there is also an option to have a sound play at certain events, for example hitting a breakpoint ("Breakpoint hit"). ( Documentation/archived, see "Play sound".)

Trying it quickly, I couldn't find the option to play a sound when an exception is hit (it is not the option "Error Report"). But enabling sound for "Breakpoint hit" for me does the job, together with enabling breakpoints when an untreated exception is hit.

According to the docs, it works for Windows, Linux and MacOs.

enter image description here

Upvotes: 0

Hadij
Hadij

Reputation: 4600

You can use this one in Windows:

import winsound
try:
    int('abc')
except Exception as e:
    winsound.PlaySound("*", winsound.SND_ALIAS)
    raise e

Replace int('abc') with your code.

NOTE: It can be used only in "Windows". Not applicable to Linux / Mac OS.

Upvotes: 3

David
David

Reputation: 1196

You can catch all errors globally and beep when an error occurs:

try:
    do_something()
except:
    winsound.Beep(440, 1000)

Upvotes: 2

Related Questions