<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type="text/javascript"> const COLOR_RED = "red"; const COLOR_YELLOW = "yellow"; const COLOR_BLUE = "blue"; const MY_BLUE = "blue"; // const COLOR_RED = Symbol("red"); // const COLOR_YELLOW = Symbol("yellow"); // const COLOR_BLUE = Symbol("blue"); function ColorException(message) { this.message = message; this.name = "ColorException"; } function getConstantName(color) { switch (color) { case COLOR_RED : return "COLOR_RED"; case COLOR_YELLOW : return "COLOR_YELLOW "; case COLOR_BLUE: return "COLOR_BLUE"; default: throw new ColorException("找不到这种颜色"); } } try { var color = "green"; // green 引发异常 var colorName = getConstantName(color); } catch (e) { var colorName = "unknown"; console.log(e.message, e.name); // 传递异常对象到错误处理 } </script> </body> </html>