1try {
2 // Try to run this code
3}
4catch(err) {
5 // if any error, Code throws the error
6}
7finally {
8 // Always run this code regardless of error or not
9 //this block is optional
10}
1try {
2 // test code
3} catch (error) { // if error
4 console.error(error); // return error
5}
1import haxe.Exception;
2
3class Main {
4 static function main() {
5 try {
6 try {
7 doSomething();
8 } catch(e:Exception) {
9 trace(e.stack);
10 throw e; //rethrow
11 }
12 } catch(e:Exception) {
13 trace(e.stack);
14 }
15 }
16
17 static function doSomething() {
18 throw new Exception('Terrible error');
19 }
20}
21
1<html>
2<head>Exception Handling</head>
3<body>
4<script>
5try {
6 throw new Error('This is the throw keyword'); //user-defined throw statement.
7}
8catch (e) {
9 document.write(e.message); // This will generate an error message
10}
11</script>
12</body>
13</html>
1First try block try to handle it
2if not then catch block will handle it.
3Finally block will executed regardless
4of the outcome
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}