I want to get a part of a string. The string can have this format:
Parte1 - Parte2[Parte3]
Or it can also have this other format:
Parte1[Parte2]
With the first format I had no problems, I was using this function and everything was fine. In this case I wanted to extract Part2.
def GetPart(string):
x = string.split("-")
y = x[1].split("[")
return y[0]
The problem appears when I want to use this function for the second format, I would like to catch the exception that is thrown conditionally so that if it can't handle the first format it will handle the second.
Something more or less like this:
def GetPart(string):
try
x = string.split("-")
y = x[1].split("[")
result = y[0]
except IndexError: pass
try
x = string.split("[")
result = y[0]
except IndexError as e:
print( "A retriable error occurred: %s" % e)
return result
I would like that if there are no exceptions in the first try to go directly to the return and not do the second try.
I tried to do it with goto like so:
def GetPart(string):
try:
x = string.split("-")
y = x[1].split("[")
result = y[0]
goto .fin
except IndexError: pass
try:
x = string.split("[")
result = y[0]
except IndexError as e:
print( "A retriable error occurred: %s" % e)
label .fin
return result
But either I'm doing it wrong or they've removed the python goto because it doesn't work.
Can this be done? And if it can be done, how is it done?
Python is a highly structured language and it is generally considered unnecessary and bad practice in these languages to use the unconditional transfer statement. In this case, Python does not directly have instructions like
goto
orjump
.These problems are solved in a simple way using structured programming features (conditionals, loops, etc). In this case it is solved simply by placing another
return
in thetry
, for example:You can have as many
return
as you want in a function and at any point (inside loops, inside conditionals, etc). The moment one is executed the function ends at that point. Although it is not usually the most indicated, in Python you can have differentreturn
ones that return different types even.Another option is to not use exception handling and use conditionals:
Edit clarifying comment question:
You can nest blocks
try-except
without problems, however, since you are inside a function and any functionreturn
terminates its execution, you can not nest them as long as each one has itsreturn
in thetry
, for example:EXHIBIT:
Catching exceptions in Python is done using the statement
try
, which basically consists of:Clause
try
: includes the code whose possible exceptions we are going to handle.Clause or clauses
except
: that contain the code to be executed in case oftry
failure. We can specify that aexcept
single catch certain types of exceptions:To catch an exception in the
except
useas
:Optional clause
else
: is executed when thetry
completes with no exceptions. In this case the contained code is not protected bytry
avoiding catching exceptions that we don't want to handle.Optional clause
finally
: always executed before terminal block, whether or not an exception occurred. When an exception has occurred in the clausetry
and has not been handled by a clauseexcept
it is re-raised after the clause has been executedfinally
. Exit statements such asbreak
,continue
orreturn
do not preventfinally
execution before executing them .The statement
pass
just "does nothing", is used when we declare a block that cannot be syntactically empty (causes aIdentationError
) likeif
,elif
,else
,except
,def
orclass
, but we don't want it to do anything, at least for now.