Baseline Widely available
throw
문ì ì¬ì©ì ì ì ìì¸ë¥¼ ë°ì(throw)í ì ììµëë¤. ìì¸ê° ë°ìíë©´ íì¬ í¨ìì ì¤íì´ ì¤ì§ëê³ (throw
ì´íì ëª
ë ¹ë¬¸ì ì¤íëì§ ììµëë¤.), ì ì´ íë¦ì ì½ì¤íì 첫 ë²ì§¸ catch
ë¸ë¡ì¼ë¡ ì ë¬ë©ëë¤. í¸ì¶ì í¨ì ì¬ì´ì catch
ë¸ë¡ì´ ìì¼ë©´ íë¡ê·¸ë¨ì´ ì¢
ë£ë©ëë¤.
function getRectArea(width, height) {
if (isNaN(width) || isNaN(height)) {
throw new Error("Parameter is not a number!");
}
}
try {
getRectArea(3, "A");
} catch (e) {
console.error(e);
// Expected output: Error: Parameter is not a number!
}
문ë²
expression
ìì¸ë¥¼ ë°ììí¬ ííì
ìì¸ë¥¼ ë°ìí기 ìí´ throw
문ì ì¬ì©íì¸ì. ìì¸ë¥¼ ë°ììí¤ë©´ expression
ì ìì¸ ê°ì ì§ì í©ëë¤. ë¤ì ê°ê°ì ìì¸ë¥¼ ë°ììíµëë¤:
throw "Error2"; // 문ìì´ ê°ì ê°ì§ë ìì¸ê° ë°ìí©ëë¤.
throw 42; // 42 ê°ì ê°ì§ ìì¸ê° ë°ìí©ëë¤.
throw true; // true ê°ì ê°ì§ë ìì¸ê° ë°ìí©ëë¤.
ëí throw
문ì ìë ì¸ë¯¸ì½ë¡ ì½ì
(ASI)ì ìí´ ìí¥ì ë°ì¼ë©° throw
í¤ìëì ííì ì¬ì´ì ì¤ ì¢
ê²°ìë íì©ëì§ ìì¼ë¯ë¡ 주ìí´ì¼í©ëë¤.
ìì¸ë¥¼ ë°ììí¬ ë ê°ì²´ë¥¼ ëª
ìí ì ììµëë¤. ê·¸ë¬ë©´ catch
ë¸ë¡ìì ê°ì²´ì ìì±ì 참조 í ì ììµëë¤. ë¤ì ìì ììë UserException
íì
ì ê°ì²´ë¥¼ ë§ë¤ê³ throw
구문ìì ì´ ê°ì²´ë¥¼ ì¬ì©í©ëë¤.
function UserException(message) {
this.message = message;
this.name = "UserException";
}
function getMonthName(mo) {
mo = mo - 1; // ì ì«ì를 ë°°ì´ì ì¸ë±ì¤ ê°ê³¼ ë§ì¶ê¸° ìí´ì ì
ëë¤.(1 = 1ì, 12 = 12ì)
var months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
if (months[mo] !== undefined) {
return months[mo];
} else {
throw new UserException("InvalidMonthNo");
}
}
try {
// try 문
var myMonth = 15; // 15 ë ë²ì를 ë²ì´ë¬ê¸° ë문ì ìì¸ë¥¼ ë°ììíµëë¤
var monthName = getMonthName(myMonth);
} catch (e) {
monthName = "unknown";
console.error(e.message, e.name); // ì¤ë¥ ì²ë¦¬ê¸°ì ìì¸ ê°ì²´ë¥¼ ì ë¬í©ëë¤
}
ìì¸ ê°ì¼ë¡ ê°ì²´ ì¬ì©íë ë¤ë¥¸ ìì
ë¤ì ìì ë ì
ë ¥ 문ìì´ìì ë¯¸êµ ì°í¸ ë²í¸ë¥¼ í
ì¤í¸í©ëë¤. ì°í¸ ë²í¸ê° ì못ë íìì ì¬ì©íë ê²½ì° throw 문ì ZipCodeFormatException
íì
ì ê°ì²´ë¥¼ ë§ë¤ì´ ìì¸ë¥¼ ë°ììíµëë¤.
/*
* ZipCode ê°ì²´ë¥¼ ë§ëëë¤.
*
* ì
ë ¥ë°ì ì ìë ì°í¸ë²í¸ì ííë ìëì ê°ìµëë¤:
* 12345
* 12345-6789
* 123456789
* 12345 6789
*
* ë§ì½ ZipCode ìì±ìë¡ ì ë¬ë 매ê°ë³ìê° ì´ í¨í´ ì¤ íëë ë§ì§ ìì¼ë©´,
* ìì¸ë¥¼ ë°ììíµëë¤.
*/
function ZipCode(zip) {
zip = new String(zip);
pattern = /[0-9]{5}([- ]?[0-9]{4})?/;
if (pattern.test(zip)) {
// ì°í¸ë²í¸ ê°ì 문ìì´ì 첫 ë²ì§¸ 매ì¹ì¼ ê²ì
ëë¤.
this.value = zip.match(pattern)[0];
this.valueOf = function () {
return this.value;
};
this.toString = function () {
return String(this.value);
};
} else {
throw new ZipCodeFormatException(zip);
}
}
function ZipCodeFormatException(value) {
this.value = value;
this.message = "does not conform to the expected format for a zip code";
this.toString = function () {
return this.value + this.message;
};
}
/*
* ì´ê²ì ë¯¸êµ ì£¼ìì ëí 주ì ë°ì´í°ë¥¼ ê²ì¦íë ì¤í¬ë¦½í¸ìì
* ë°ìí ì ììµëë¤.
*/
const ZIPCODE_INVALID = -1;
const ZIPCODE_UNKNOWN_ERROR = -2;
function verifyZipCode(z) {
try {
z = new ZipCode(z);
} catch (e) {
if (e instanceof ZipCodeFormatException) {
return ZIPCODE_INVALID;
} else {
return ZIPCODE_UNKNOWN_ERROR;
}
}
return z;
}
a = verifyZipCode(95060); // 95060 ë°í
b = verifyZipCode(9560); // -1 ë°í
c = verifyZipCode("a"); // -1 ë°í
d = verifyZipCode("95060"); // 95060 ë°í
e = verifyZipCode("95060 1234"); // 95060 1234 ë°í
ìì¸ ë¤ì ë°ììí¤ê¸°
throw
를 ì¬ì©íì¬ ìì¸ë¥¼ ì¡ì(catch) íì ìì¸ë¥¼ ë¤ì ë°ììí¬ ì ììµëë¤. ë¤ì ìì ììë ì«ì ê°ì¼ë¡ ìì¸ë¥¼ ì¡ì§ë§ ê°ì´ 50 ì´ìì´ë©´ ìì¸ë¥¼ ë¤ì ë°ììíµëë¤. ë°íë ìì¸ë ëë¬ì¸ë í¨ì ëë ìµìì ìì¤ì¼ë¡ ì íëì´ ì¬ì©ìê° ë³¼ ì ìëë¡í©ëë¤
try {
throw n; // ì«ì ê°ì¼ë¡ ìì¸ë¥¼ ë°ììíµëë¤.
} catch (e) {
if (e <= 50) {
// 1-50 ì¬ì´ì ìì¸ë¥¼ ì²ë¦¬íë 구문
} else {
// ì´ ìì¸ë¥¼ ì²ë¦¬í ì ìì´ì, ë¤ì ìì¸ë¥¼ ë°ììíµëë¤
throw e;
}
}
ëª
ì¸ ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4