Ravanelli
Ravanelli

Reputation: 95

Break the try and go directly to the exception if condition is false

I have a complex code in python with condition inside try:

try:
   if condition is True: execute the code 
   else: go to exception and don't execute the code
except:
   execute the except statement

I want to exit the try statement if condition is met

Upvotes: 0

Views: 1022

Answers (1)

Murgalha
Murgalha

Reputation: 378

Just manually raise an exception:

try:
    if condition:
        your code
    else:
        raise Exception('Exception message')
except:
    # except code here

Upvotes: 4

Related Questions