JavaScript Tutorials
JavaScriptThrow Exception in JavaScript
Codes in an application could be regarded as the rules in dealing with data and processes. With codes, developers are able to guide the data and process them through different formulas. In any programming languages, codes will help determine the outcome of the application.
However, not all rules imposed by codes are universal. Sometimes, there is information that shouldn’t be accepted. This type of information is dubbed as exception. With an exception, certain information will be treated differently. It could be used to alert the user that certain information is not accepted or will redirect the user to another webpage.
JavaScript also accepts exception. Instead of extended coding for simple information, developers will just have to use the function called “throw”. This function will inform the JavaScript that the application will treat the information differently. It’s a simplified to react to specific information and will eventually provide the user additional information for assistance such as a prompt or redirection to another webpage.
JavaScript “throw” goes this way:
if(data)
throw “function”
You may notice that “if” comes with a data. The function “throw” is primarily used to interact with data. User actions could be used as an exception but it will defeat the purpose of “throw” since it should be used to provide additional information to users instead of redirecting the user to another webpage. The developer still has to use the traditional coding in JavaScript when dealing with user actions.
The “function” required after the throw could be a simple pop-up or an alert or a redirection to another webpage. But the function is not written entirely after “throw. The “throw” will only be connected to a code that represents that action. That code represents and argument which will lead to an action. Here’s an example:
{
if(data)
throw “function”
else if(data)
throw “function1”
}
catch(func)
{
if(func= ="function")
alert("Reloading Unsuccessful");
if(func= ="function1")
alert("Reloading Unsuccessful");
}
Notice that “function” and “function1” cannot be executed on its own. The catch function will alert the JavaScript that there are rules below that should be followed. The (func) is a common term in both functions so it could be used to easily identify the commands if an exception happens.
The developers could write as many exceptions as they can as long as it is used to deal with data. The challenge is only based on the fact that since it’s only data, a lot of exceptions might happen.
Developers are often tempted to use “throw” in any small data exception which could become a very long code. Thus, it is important to know where to apply “throw” so that the application will benefit from this function instead of being burdened by it.
A simple tip: avoid using uppercase for the word “throw”. For strange reasons, the uppercase cannot be read and an error usually happens. A lowercase should be used as much as possible to ensure proper execution.
The function “throw” might be a simple syntax but it could significantly increase user experience as long as developers know where to apply them.
Sponsored Links
