Python Forum
best way to force an exception
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
best way to force an exception
#1
in a try part of a try/except i want a certain conditional test to run the except part. it seems the simple way to do that is to throw an exception the except part will catch. what is the best or most pythonic way to do that?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I would assume you'd just raise the exception that you want to catch.

debug = True
try:
    # run the exception
    if debug:
        raise ZeroDivisionError
except ZeroDivisionError:
    print("Got division error without actually dividing")

print("all done")
Reply
#3
skaperen Wrote:what is the best or most pythonic way to do that?
There is no best way unless you show some real code. This question needs some context.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  force a program to exit ? Armandito 3 2,513 May-12-2022, 04:03 PM
Last Post: Gribouillis
  Force calculation result as decimal vercetty92 4 2,856 Mar-20-2019, 02:27 PM
Last Post: vercetty92
  During handling of the above exception, another exception occurred Skaperen 7 26,869 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  Password Brute Force 2skywalkers 9 5,344 Oct-18-2018, 02:35 PM
Last Post: buran
  Brute Force Password Guesser 2skywalkers 1 3,172 Oct-05-2018, 08:04 PM
Last Post: ichabod801
  Brute Force Pad Lock Guesser RedSkeleton007 4 3,927 Mar-03-2018, 07:42 AM
Last Post: RedSkeleton007
  brute force password from list petru 10 11,953 Apr-04-2017, 02:08 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020