Question: When I intentionally shut down a web sockets server the client JavaScript code:

```
try
{
    self.socket.send('ping');
}
catch(ex)
{
    console.log('exception');
}

```

writes in Chrome console:

```
WebSocket is already in CLOSING or CLOSED state.

```

This exception couldn't be caught by try-catch. How to detect for sure that data could not be sent through web socket? onclose event is not being fired.

Answer: 
`file.write` is given multiple arguments when its expecting only one string argument

```
file.write('stem total frequency:', s)
                                  ^

```

The error gets raised as `'stem total frequency:', s` are treated as two different arguments. This can be fixed by concatenation

```
file.write('stem total frequency: '+str(s))
                                  ^

```
```
file.write('stem:', k)

```

You're supplying two arguments to `write` on this line, when it only wants one. In contrast, `print` is happy to take as many arguments as you care to give it. Try:

```
file.write('stem: ' + str(k))

```