oz1cz
7
Q:

try catch error

try {
  // Try to run this code 
}
catch(err) {
  // if any error, Code throws the error
}
finally {
  // Always run this code regardless of error or not
  //this block is optional
}
24
// 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
    }
}
1
try {
  // test code
} catch (error) { // if error
  console.error(error); // return error
}
0
<html>  
<head>Exception Handling</head>  
<body>  
<script>  
try {  
   throw new Error('This is the throw keyword'); //user-defined throw statement.  
}  
catch (e) {  
  document.write(e.message); // This will generate an error message  
}  
</script>  
</body>  
</html>  
0
import haxe.Exception;

class Main {
  static function main() {
    try {
      try {
        doSomething();
      } catch(e:Exception) {
        trace(e.stack);
        throw e; //rethrow
      }
    } catch(e:Exception) {
      trace(e.stack);
    }
  }

  static function doSomething() {
    throw new Exception('Terrible error');
  }
}
0
try {
console.log('hello');
}
catch (e){
}
-1

 <p id="demo"></p>

<script>

try {
  adddlert("Welcome guest!");
}
catch(err) {
  
document.getElementById("demo").innerHTML = err.message;
}
</script> 
-1

Tags

New to Communities?

Join the community