Is it possible to evaluate multiple conditions in a try block in Python. Below is the case. I have 2 conditions below.
- Connect to sql server to read data into two dataframes. There is a timeout in the code, if the connection takes more than 15 seconds the code should raise an exception and exit.
- Check if these two dataframe have data.If either one of the dataframes is empty, exit the code, if not continue the code in the else block.
I am currently thinking of doing like this. Is there more elegant way.
try:
    #Condition 1
except:
    #Condition 1
try:
    #Condition 2
except:
     #Condition 2
else:
    #Condition 3
def connect_to_server(table):
    # Connection Code
    server = '' 
    username = '' 
    password = '' 
    database = ''
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';ENCRYPT=yes;UID='+username+';PWD='+ password)
    cnxn.timeout = 5   
    cursor = cnxn.cursor()
    try:
        cnxn.execute('SELECT * FROM ' +table)        
    except Exception as my_timeout_exception:
        raise my_timeout_exception
        
        
def read_database(table):
    server = '' 
    username = '' 
    password = '' 
    database = ''
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';ENCRYPT=yes;UID='+username+';PWD='+ password)
    cursor = cnxn.cursor()
    df = pd.read_sql('SELECT * FROM '  +table, cnxn)
    if df.empty:
        print("dataframe is empty")
    else:
        return df
        
        
try:
    using_con = connect_to_server('table')
    df = read_database('table')
except my_timeout_exception:
    handle_error_1
    #break
except empty_df_exception: 
    handle_error_2
    #break
else:
    print("z")
 
    