{
try {
console.log("hi");
throw new Error("error message is hi");
} catch (e) {
if (e instanceof RangeError) {
console.error(e.message);
} else {
throw e;
}
}
}
{
class ValidationError extends Error {
constructor(message: string) {
super(message);
this.name = "ValidationError";
}
}
class PropertyRequiredError extends ValidationError {
property: string;
constructor(property: string) {
super("No property: " + property);
this.name = "PropertyRequiredError";
this.property = property;
}
}
function readUser(json: any) {
let user = JSON.parse(json);
if (!user.age) {
throw new PropertyRequiredError("age");
}
if (!user.name) {
throw new PropertyRequiredError("name");
}
return user;
}
try {
let user = readUser('{"age":25}');
} catch (e) {
if (e instanceof ValidationError) {
console.error("Invalid data: " + e.message);
console.error(e.name);
} else if (e instanceof SyntaxError) {
console.error("JSON Syntax Error: " + e.message);
} else {
throw e;
}
}
}
{
class MyError extends Error {
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
class ValidationError extends MyError {}
class PropertyRequiredError extends ValidationError {
property: string;
constructor(property: string) {
super("No property: " + property);
this.property = property;
}
}
console.log("name???????????", new PropertyRequiredError("field").name);
}