> For the complete documentation index, see [llms.txt](https://george-jen.gitbook.io/data-science-and-apache-spark/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://george-jen.gitbook.io/data-science-and-apache-spark/try-except.md).

# try except

To handle exception that you foresee, use try except

```
try:
    f=open('/tmp/xyz.txt')
    contents=f.read()
    f.close()
except:
    print("something wrong")


```

create a file

echo "hello world\~wonderful" > /tmp/xyz.txt

Then run above Python code, then continue below

```
contents.strip('\n').split('~')
#['hello world', 'wonderful']
```
