2

i have never done anything serious with python, but i found it very handy for what i wanted to do.
I'd like to ask a simple question, how do i raise a custom exception if the third party class already raises it? I am using This class to make asyncronous sql query on my database, and it raises "UNIQUE constraint failed" which is fine for me but i don't want it to log it.

This is my simple code

 try:
    querypost = "INSERT INTO post(link,Commenti,data) VALUES('"+link+"','"+str(n_commenti)+"','"+str(data)+"')"
    sql_worker.execute(querypost);
except (IntegrityError) as error:
    print("test")

The problem with this is that it never reaches my print and it prints his own excpetion.
How do i make it shut up?

EDIT:

This piece of code made the logger suppress the unwanted message:
logging.getLogger("sqlite3worker").setLevel(logging.CRITICAL)

Thanks to @AChampion

Andreawu98
  • 45
  • 1
  • 6
  • Possible duplicate of [Proper way to declare custom exceptions in modern Python?](https://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python) – user3483203 Jul 06 '18 at 14:03
  • @user3483203: they are not asking about custom exceptions here. – Martijn Pieters Jul 06 '18 at 14:06
  • If the `except IntegrityError` block is not triggered, then you are not caching the right exception. It may have the same name, but it's not the exact same class. Perhaps you imported it from the wrong location, but we can't tell without more details. What does `except Exception as error: print(error, type(error))` produce? Where is `IntegrityError` imported from? – Martijn Pieters Jul 06 '18 at 14:08
  • The library you link to [raises exceptions from the `sqlite3` module](https://docs.python.org/3/library/sqlite3.html#exceptions), so catching `sqlite.IntegrityError` would do. – Martijn Pieters Jul 06 '18 at 14:09
  • 1
    @MartijnPieters it looks like the called code is catching the exception and logging it and not re-raising it (because it is making the sql query in a separate thread). OP: You can define the logger for `'sqlite3worker'` to suppress output from this code. – AChampion Jul 06 '18 at 14:12
  • I just tried removing the try-except block and it still raise it alone printing the unwanted message – Andreawu98 Jul 06 '18 at 14:12

0 Answers0