Skip to content

Instantly share code, notes, and snippets.

View camelcaseblog's full-sized avatar

camelcaseblog

View GitHub Profile
class A {
f = () => { console.log(1); }
g = () => setTimeout(() => this.f(), 1000)
}
function f() { console.log(2); }
var a = new A()
a.g()
> 1
>>> f'{7:03}'
'007'
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("a", 1);
map1.put("b", 2);
TreeMap<String, Object> map2 = new TreeMap<String, Object>();
map2.put("b", 2)
map2.put("a", 1);
map1.equals(map2); // true
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);
}
try {
const y = x
} catch(err) {
console.log(err.name + ': ' + err.message);
}
> ReferenceError: x is not defined
try {
throw “this is an error”;
} catch(err) {
console.log(err.name + ': ' + err.message);
}
> undefined: undefined
try {
throw new Date();
} catch(err) {
console.log(err);
}
Tue Jun 25 2019 19:12:20 GMT+0300 (Israel Daylight Time)
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");
var e = new Error();
e.name = ‘MyError’;
throw e;
// is equivalent to:
class MyError extends Error {}
throw MyError();