xxxxxxxxxx
// 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 minimum\n";
else if (val1 > val2)
cout << val2 << " is the minimum\n";
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 maximum\n\n";
else if (val1 < val2)
cout << val2 << " is the maximum\n\n";
else
throw 505; // exception error processing when input is invalid
}
catch (T random_num)
{
cout << "Error 505!\n\n"; // Second part of error messagee
}
}
xxxxxxxxxx
try {
try_statements
}
catch (exception_var) {
catch_statements
}
finally {
finally_statements
}
xxxxxxxxxx
async function promHandler<T>(
prom: Promise<T>
): Promise<[T | null, any]> {
try {
return [await prom, null];
} catch (error) {
return [null, error];
}
}
xxxxxxxxxx
class Main {
public static void main(String[] args) {
try {
// code that generate exception
int divideByZero = 5 / 0;
System.out.println("Rest of code in try block");
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage());
}
}
}
xxxxxxxxxx
gooi: function () {
try {
if (this.balPositie !== "links") {
throw Error("bal in verkeerde positie")
}
this.draw(300, 50);
this.balPositie = "midden";
} catch {
var bericht = "fout, bal al in de lucht of al gevangen";
document.getElementById("melding").innerHTML = bericht;
}
},
xxxxxxxxxx
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
xxxxxxxxxx
public class Question6 {
public static void main(String[] args){
try {
int n1, n2; // The values of s1 and s2 as integers.
int sum; // The sum of n1 and n2.
String s1 = "3";
String s2 = "4";
n1 = Integer.parseInt(s1);
n2 = Integer.parseInt(s2);
sum = n1 + n2; // (If an exception occurs, we don't get to this point.)
System.out.println("The sum is " + sum);
}
catch ( NumberFormatException e ) {
System.out.println("Error! Unable to convert strings to integers.");
}