Answers for "how to use try and except"

22

python try catch

try:
  # Dangerous stuff
except ValueError:
  # If you use try, at least 1 except block is mandatory!
  # Handle it somehow / ignore
except (BadThingError, HorrbileThingError) as e:
  # Hande it differently
except:
  # This will catch every exception.
else:
  # Else block is not mandatory.
  # Dangerous stuff ended with no exception
finally:
  # Finally block is not mandatory.
  # This will ALWAYS happen after the above blocks.
Posted by: Guest on June-25-2020
3

try except

try:
    print("I will try to print this line of code")
except:
    print("I will print this line of code if an error is encountered")
else:
    print("I will print this line of code if there's no error encountered")
finally:
    print("I will print this line of code even if there's an error or no error encountered")
Posted by: Guest on September-03-2021
0

error handling in python

try:
  print(x)
except SyntaxError:
  print("There is a SyntaxError in your code")
except NameError:
  print("There is a NameError in your code")
except TypeError:
  print("There is a TypeError in your code")
Posted by: Guest on September-08-2020
0

try and catch

try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("[email protected]");
                mail.To.Add("to_address");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
Posted by: Guest on August-28-2021
0

try and exception

def loan_emi(amount, duration, rate, down_payment=0):
    loan_amount = amount - down_payment
    try:
        emi = loan_amount * rate * ((1+rate)**duration) / (((1+rate)**duration)-1)
    except ZeroDivisionError:
        emi = loan_amount / duration
    emi = math.ceil(emi)
    return emi
Posted by: Guest on July-18-2021

Python Answers by Framework

Browse Popular Code Answers by Language