Skip to content

Instantly share code, notes, and snippets.

View camelcaseblog's full-sized avatar

camelcaseblog

View GitHub Profile
class MyError extends Error {}
try {
doStuff();
} catch(err) {
if (err.name == 'MyError') {
console.log("This is my error");
} else if (err.name == 'ReferenceError') {
console.log("Not my error. Ask the ReferenceError guys");
} else {
console.log("I have no idea");
try {
throw new Date();
} catch(err) {
console.log(err);
}
Tue Jun 25 2019 19:12:20 GMT+0300 (Israel Daylight Time)
try {
throw “this is an error”;
} catch(err) {
console.log(err.name + ': ' + err.message);
}
> undefined: undefined
try {
const y = x
} catch(err) {
console.log(err.name + ': ' + err.message);
}
> ReferenceError: x is not defined
try:
ratio = a / b
except ZeroDivisionError:
ratio = float(‘inf’)
except TypeError as e:
logger.critical(‘Got TypeError: %s’, e)
try {
System.out.println(obj.charAt(0));
} catch(NullPointerException e) {
System.out.println("NullPointerException..");
} catch(MyAwesomeCustomException e) {
System.out.println("Caught an exception: " + e.myCustomInformation);
}
function createPrintFunction(i) {
return function() { console.log(i); }
}
var functions = []
for (var i = 0; i < 10; i++){
functions.push(createPrintFunction(i))
}
functions.forEach(f => f())
var functions = []
for (var i = 0; i < 10; i++){
functions.push(() => console.log(i))
}
functions.forEach(f => f())
class A {
f = () => { console.log(1); }
g = () => setTimeout(() => this.f(), 1000)
}
function f() { console.log(2); }
var a = new A()
a.g()
> 1
class A {
f() { console.log(1); }
g(){ setTimeout(function() { this.f(); }, 1000)}
}
function f() { console.log(2); }
var a = new A()
a.g()
> 2