please dont rip this site

JavaScript Error Handling: Try Catch Throw Finally

try {
    //statements that may cause an error
    }
catch (err) { //err holds info about the error
    //statements that handle an error, if one happens
    throw err //if you decide to error after all
    }
finally { //optional
    //statements that happen error or not
    }

There are 3 types of errors:

  1. Programmer messed up. Common.
  2. Programmer didn't catch every possible case of bad data, and bad data causes an error. Occasional.
  3. Programmer made no mistakes and the machine choked. Very very rare.

#1 is corrected by the programmer learning and fixing the code. #2 can be fixed by adding more code to better check the data, but it starts to get annoying... #3 can't be fixed... except by a try-catch block. And, they really manes dealing with bad data easy. Just don't deal with it, and reject anything that causes a crash.

The catch block can example the error object and take action based on the type of the error. e.g.

try {
  something();
} catch (e) {
  if (e instanceof RangeError) {
    // statements to handle this very common expected error
    console.log("out of range"+e)
    throw "value out of range"
  } else {
    throw e;  // re-throw the error unchanged
  }
}

The error object also has two attributes: .name which is the general class of error, and .message which is a short error message.

Error classes include:

The throw keyword accepts any expression, or can be used with new to make a new error object of any error class or a generic error. E.g.

throw "bogus" //directly using a string or other value
throw my_error_messages(32) //function can return a value for the error
throw new Error("bogus") //generic error with message
throw new TypeError("value must be a widget") //error of type TypeError

See also:


file: /Techref/language/java/script/try-catch-finally.htm, 2KB, , updated: 2020/3/1 23:27, local time: 2024/5/25 07:54,
TOP NEW HELP FIND: 
3.238.112.198:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://www.piclist.com/techref/language/java/script/try-catch-finally.htm"> JavaScript Try Catch Finally</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host massmind.org, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
 

Welcome to www.piclist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .