1First try block try to handle it
2if not then catch block will handle it.
3Finally block will executed regardless
4of the outcome
1def loan_emi(amount, duration, rate, down_payment=0):
2 loan_amount = amount - down_payment
3 try:
4 emi = loan_amount * rate * ((1+rate)**duration) / (((1+rate)**duration)-1)
5 except ZeroDivisionError:
6 emi = loan_amount / duration
7 emi = math.ceil(emi)
8 return emi
1// Main program passes in two ints, checks for errors / invalid input
2// using template class type T for all variables within functions
3#include <iostream>
4using namespace std;
5
6template <class T> // make function return type template (T)
7void getMin(T val1, T val2)
8{
9 try
10 {
11 if (val1 < val2) // if val1 less than return it as min
12 cout << val1 << " is the minimum\n";
13 else if (val1 > val2)
14 cout << val2 << " is the minimum\n";
15 else
16 throw 505; // exception error processing when input is invalid
17 }
18 catch(T my_ERROR_NUM)
19 {
20 cout << "Input is invalid, try again. "; // first part of error message
21 }
22}
23
24template <class T>
25void getMax(T val1, T val2) // make function return type template (T)
26{
27 try
28 {
29 if (val1 > val2) // if val1 greater then return it as max
30 cout << val1 << " is the maximum\n\n";
31 else if (val1 < val2)
32 cout << val2 << " is the maximum\n\n";
33 else
34 throw 505; // exception error processing when input is invalid
35 }
36 catch (T random_num)
37 {
38 cout << "Error 505!\n\n"; // Second part of error messagee
39 }
40}