Avoid errors in cases where ``traceback`` is None

When logging of exceptions is used, in some cases ``traceback`` can be ``None``, which caused the following error:

```
AttributeError: 'NoneType' object has no attribute 'tb_next'
```

This patch fixes this error.