Home > database >  Change Traceback (most recent call last) to display "error: website is unavailable"
Change Traceback (most recent call last) to display "error: website is unavailable"

Time:06-28

I found Python as a hobby so now I'm trying to fix a problem.

When a website is not responding (HTTP Error 503: Service Unavailable), I get

Traceback (most recent call last):
  File "c:\Users\dawid\Studia\Programowanie\HTML.py", line 30, in <module>
    start()
  File "c:\Users\dawid\Studia\Programowanie\HTML.py", line 21, in start
    response = browser.open(oceny)
  File "C:\Users\dawid\Studia\Programowanie\.venv\lib\site-packages\mechanize\_mechanize.py", line 257, in open
    return self._mech_open(url_or_request, data, timeout=timeout)
  File "C:\Users\dawid\Studia\Programowanie\.venv\lib\site-packages\mechanize\_mechanize.py", line 313, in _mech_open
    raise response
mechanize._response.get_seek_wrapper_class.<locals>.httperror_seek_wrapper: HTTP Error 503: Service Unavailable

How can I change it to display something like "There is a HTTP Error: 503 - wait a few minutes"?

CodePudding user response:

The best way to do this is using a try-except.

The syntax is pretty easy:

try:
    # code that throws exception here
except:
    print("There is a HTTP Error: 503 - wait a few minutes")

You can add specific Exceptions and Errors into the except clause, if you maybe have to handle different exceptions. If left blank, you will handle every error. In case, you can also add a finally if you have to close a connection after the exception raise,, like so:

try:
    # code that throws exception here
except:
    print("There is a HTTP Error: 503 - wait a few minutes")
finally:
    # code that closes everything safely
  • Related