![]() |
||
|
aranea.ru || JavaScript - Введение | throw
throwОписаниеГенерирует условие ошибки, которое можно обработать с помощью оператора try...catch. Синтаксисthrow exceptionАргумент exception может быть любым выражением. КомментарийСледующий пример перемещает ошибку, основанную на значении выполнения, и, затем, показывает, как эта ошибка обрабатывается в иерархии оператора try...catch: function TryCatchDemo(x) { try { try { if (x == 0) // Evalute argument. throw "x equals zero"; // Throw an error. else throw "x does not equal zero"; // Throw a different error. } catch(e) { // Handle "x = 0" errors here. if (e == "x equals zero") // Check for an error handled here. return(e + " handled locally."); // Return object error message. else // Can't handle error here. throw e; // Rethrow the error for next } // error handler. } catch(e) { // Handle other errors here. return(e + " handled higher up."); // Return error message. } } document.write(TryCatchDemo(0)); document.write(TryCatchDemo(1)); |