# Stop script when there is error

**URL:** <https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951>\
**Category:** Using Streamlit\
**Created:** [July 1, 2020, 10:44pm UTC](https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951 "2020-07-01T22:44:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![7adityaraj](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/7adityaraj/32/2533_2.png) [@7adityaraj](https://discuss.streamlit.io/u/7adityaraj)\
**Post date:** [July 1, 2020, 10:44pm UTC](https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951/1 "2020-07-01T22:44:55Z")

</div>

Hello All,

I have two questions which I will create two post separately.

1. When there is an error or expected exit, what can be used to exit the script.  
As I see exit(). sys.exit() are not useful in streamlit.

## I tried something from: [https://gist.githubusercontent.com/treuille/bc4eacbb00bfc846b73eec2984869645/raw/734a2522d68920dfd1c875f321df67b3168afbf5/confirm\_button\_hack.py](https://gist.githubusercontent.com/treuille/bc4eacbb00bfc846b73eec2984869645/raw/734a2522d68920dfd1c875f321df67b3168afbf5/confirm_button_hack.py)

class StopExecution(Exception):  
“”“Special Exception which does not display any output to the Streamlit app.”""  
pass

```
try:
	fl= open(textfile,'r', encoding = "utf-8")
except StopExecution:
	st.write (' text file doesnot exist.. exiting')
	pass

if 'string' in open(textfile).read():
		st.write ("")
else:
	st.write ('file is empty.. exiting')
       raise StopExecution

```

* * *

Tried several pattern but I was not successful. Anyone can please suggest if this is correct or am I missing something.

---

<div class="post-metadata">

**Author:** ![7adityaraj](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/7adityaraj/32/2533_2.png) [@7adityaraj](https://discuss.streamlit.io/u/7adityaraj)\
**Post date:** [July 2, 2020, 10:54am UTC](https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951/2 "2020-07-02T10:54:02Z")

</div>

I have also tried

st.stop()

&

raise st.ScriptRunner.StopException

None of them works for me.  
Any suggestion would be appreciated.

> streamlit --version  
> Streamlit, version 0.62.1

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [July 2, 2020, 1:15pm UTC](https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951/3 "2020-07-02T13:15:34Z")

</div>

Hi @7adityaraj, welcome to the Streamlit community!

For a web app, you generally don’t want to stop the app. What use case are you imagining?

Best,  
Randy

---

<div class="post-metadata">

**Author:** ![7adityaraj](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/7adityaraj/32/2533_2.png) [@7adityaraj](https://discuss.streamlit.io/u/7adityaraj)\
**Post date:** [July 2, 2020, 2:05pm UTC](https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951/4 "2020-07-02T14:05:21Z")

</div>

Hi @randyzwitch,

Thank you for the comment.

I am a beginner here and was trying to run mytest python script via web.

The script usually take a zip file as input and then unzips it and look for specific text file (mytest.txt) and then parse the string that it matches and prints out.

So in the script for below two cases I am willing to get error message and exit the script.

1. When text file doesn’t exist.
2. When text file is blank.

This works from python:

```
> try:
> fl= open(textfile,'r', encoding = "utf-8")
> except:
> print ('Text file doesnot exist.. exiting')
> exit ()

> if 'string1' in open(textfile).read():
> print ("")
> else:
> print ('Text file is empty.. exiting')
> exit ()
> so on.....

```

I apologise in advance if provided informations are not clear.

---

<div class="post-metadata">

**Author:** ![7adityaraj](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/7adityaraj/32/2533_2.png) [@7adityaraj](https://discuss.streamlit.io/u/7adityaraj)\
**Post date:** [July 6, 2020, 8:28am UTC](https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951/5 "2020-07-06T08:28:47Z")

</div>

I have found a way… thank you.

---

<div class="post-metadata">

**Author:** ![Vincent-Cui](https://avatars.discourse-cdn.com/v4/letter/v/0ea827/32.png) [@Vincent-Cui](https://discuss.streamlit.io/u/Vincent-Cui)\
**Post date:** [August 23, 2020, 4:09pm UTC](https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951/6 "2020-08-23T16:09:35Z")

</div>

So whats that? I am experiencing the same problem!

---

<div class="post-metadata">

**Author:** ![linhYedda](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/linhyedda/32/6930_2.png) [@linhYedda](https://discuss.streamlit.io/u/linhYedda)\
**Post date:** [April 5, 2021, 4:30pm UTC](https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951/7 "2021-04-05T16:30:18Z")

</div>

try with  
import os  
os.\_exit(0)  
st.stop()

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [May 13, 2022, 9:17pm UTC](https://discuss.streamlit.io/t/stop-script-when-there-is-error/3951/8 "2022-05-13T21:17:25Z")

</div>


