try catch block code example
Example 1: java try catch
try {
} catch(ErrorName e){
}
Example 2: try block in java
try {
}
catch(Exception e) {
}
Example 3: try catch java
public class MyClass {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3, 4, 5, 6};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong. check again");
}
}
}
Example 4: try catch haxe
import haxe.Exception;
class Main {
static function main() {
try {
try {
doSomething();
} catch(e:Exception) {
trace(e.stack);
throw e;
}
} catch(e:Exception) {
trace(e.stack);
}
}
static function doSomething() {
throw new Exception('Terrible error');
}
}
Example 5: try catch error
try {
console.log('hello');
}
catch (e){
}
Example 6: try catch block
const input = require('readline-sync');
let animals = [{name: 'cat'}, {name: 'dog'}];
let index = Number(input.question("Enter index of animal:"));
try {
console.log('animal at index:', animals[index].name);
} catch(err) {
console.log("We caught a TypeError, but our program continues to run!");
console.log("You tried to access an animal at index:", index);
}
console.log("the code goes on...");