Answers for "try except catch"

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
6

try catch exception

First try block try to handle it
if not then catch block will handle it.
Finally block will executed regardless
of the outcome
Posted by: Guest on January-05-2021
0

try catch

async function promHandler<T>(
  prom: Promise<T>
): Promise<[T | null, any]> {
  try {
    return [await prom, null];
  } catch (error) {
    return [null, error];
  }
}
Posted by: Guest on September-23-2021
0

try catch error

// Main program passes in two ints, checks for errors / invalid input
// using template class type T for all variables within functions
#include <iostream>
using namespace std;

template <class T> // make function return type template (T)
void getMin(T val1, T val2)
{
    try
    {
        if (val1 < val2) // if val1 less than return it as min
            cout << val1 << " is the minimumn";
        else if (val1 > val2)
            cout << val2 << " is the minimumn";
        else 
            throw 505; // exception error processing when input is invalid
    }
    catch(T my_ERROR_NUM)
    {
        cout << "Input is invalid, try again. "; // first part of error message
    }
}

template <class T>
void getMax(T val1, T val2) // make function return type template (T)
{
    try
    {
        if (val1 > val2) // if val1 greater then return it as max
            cout << val1 << " is the maximumnn";
        else if (val1 < val2)
            cout << val2 << " is the maximumnn";
        else
            throw 505; // exception error processing when input is invalid
    }
    catch (T random_num)
    {
        cout << "Error 505!nn"; // Second part of error messagee
    }
}
Posted by: Guest on November-26-2020
-2

python: raise error

class MyError(TypeError):
    pass

raise MyError('An error happened')
Posted by: Guest on August-10-2020

Python Answers by Framework

Browse Popular Code Answers by Language