tailieunhanh - JavaScript FOR ™ DUMmIES phần 9

. Biết, quá, mà nhiều người lướt từ chối xem xét lại các trang web mà bắn phá với các cửa sổ pop-up quảng phím liên quan đến sự kiện xảy ra khi một người dùng nhấn một phím trong khi một trang web được tải. Ngoài ra để nắm bắt được sự kiện bấm phím tổng thể, bạn có thể nắm bắt một cách riêng biệt | Chapter 17 Ten Or So Tips for Debugging Your Scripts 285 return months monthNumber Otherwise an exception occurred so throw an exception. else This statement throws an error directly to the catch block. throw InvalidMonthNumber The try block wraps around the main JavaScript processing code. Any JavaScript statement inside the try block that generates an exception will automatically throw that exception to the exception handling code in the catch block. The try block try Call the getMonthName function with an invalid month there is no 13th month and see what happens. alert getMonthName 13 alert We never get here if an exception is thrown. The catch block catch error alert An error exception was encountered. Please contact the program vendor. In a real-life situation you might want to include error-handling code here that examines the exception and gives users specific information or even tries to fix the problem if possible . Take a look at Figure 17-4 to see the error that running the code in Listing 17-2 generates in Internet Explorer. 286 Part V The Part of Tens__ Figure 17-4 The catch block handles all exceptions generated in the try block. The first code executed in Listing 17-2 is the code that you see defined in the try block alert getMonthName 13 Because only 12 months are defined in the months array passing a value of 13 to getMonthName causes an exception InvalidMonthNumber to be thrown as shown here function getMonthName monthNumber . throw InvalidMonthNumber All thrown exceptions are processed automatically by whatever code exists in the catch block so the message that you see in Figure 17-4 appears automatically when the exception is thrown. If you want to write truly airtight JavaScript code you need to identify all the events that could possibly cause an exception in your particular script such as actions the user could take error conditions the operating system could generate and so on .

TỪ KHÓA LIÊN QUAN