Answers for "try except and finally"

2

error handling Pyhtin

# taking input from the user 
try:
    age = int(input("Please enter your age: "))
    print(age)
except:
    print("please enter a number instead! ")
    print('bye')


# if you wanna ask their age until they enter the correct number use While Loop
while True:
    try:
        age = int(input("Please enter your age: "))
        print(age)
    except:
        print("please enter a number instead! ")
    else:
        break
Posted by: Guest on January-10-2021
14

python exception

try:
  # code block
except ValueError as ve:
  print(ve)
Posted by: Guest on March-31-2020
0

try-catch-finally

localStorage.getItem(key)

    try {
        data = JSON.parse(key)
    }
    catch (e) {
        // if the code errors, this bit of code will run
    }
    finally {
    
        return data
    }
Posted by: Guest on April-26-2021
0

try catch finally example

try {
  myroutine(); // may throw three types of exceptions
} catch (e) {
  if (e instanceof TypeError) {
    // statements to handle TypeError exceptions
  } else if (e instanceof RangeError) {
    // statements to handle RangeError exceptions
  } else if (e instanceof EvalError) {
    // statements to handle EvalError exceptions
  } else {
    // statements to handle any unspecified exceptions
    logMyErrors(e); // pass exception object to error handler
  }
}
Posted by: Guest on June-08-2021

Python Answers by Framework

Browse Popular Code Answers by Language