   How is a throw statement useful?
  ...Because as far as I can tell, it seems to just be something that generates a run-time error in your program unless you make a statement to catch it...but what''s the point?Can someone tell some practical uses for throw, and how to use it? 
  The idea is to throw exceptions that need, or at least should, be handled so that the program has a better chance of operating normally, if possible. If a spacecraft navigation system can inform the user there was an error, even if unrecoverable, then that is better than just dumping the stack and guiding the craft into the sun.   blah blah blah. ;  The throw statement is used for generating exceptions, which allow you to separate error handling from application logic:try{  // application logic here}catch( ... )  // catch-all{  // error handling here}  Because a try clause can be followed by multiple catch clauses (which are processed in order in terms of matching the exception argument), you can have as much error handling as is necessary for the given try block in one place instead of interspersed throughout the logic itself.Furthermore, exceptions cause the stack to unwind (this is why we debate whether things are exception safe, etc); this gives destructors a chance to execute, memory a chance to be deallocated and so forth. Many languages include a single, optional finally clause after the catch clauses. This clause is executed whenever exiting the try, exception or no. It is not yet standard (afaik) in C++, but MSVC provides it as an extension. ;  I still dont'' really get it. Can someone write another little example of a practical use for it? Maybe add lots of comments to the code, too? Sorry for the stupid questions. >_< ;quote:Original post by Krak  Can someone write another little example of a practical use for it? Maybe add lots of comments to the code, too?Did you Google it? ;  Like this? #include <iostream.h>int main(){int myArray[10];try //Start of try block{for(int i=0;i<19;i++){cout<< "Printed "<<i<<" times."<<endl; //Printsif (i>10) // if i>10,throw i; //Throw an error//(this keeps it from writing past the last element of myArray[]myArray[i]=i; //Assigns myArraycout<<"myArray["<<i<<"]: "<<i<<endl; // prints myArray[]}} //End of try blockcatch(int error)//Executes if error is thrown.{cout << "\nError: "<<error<<endl;}return 0; //End of program};  @Krak:You shouldn''t be using <iostream.h>. You should be using plain <iostream> instead. ;  Trust me, it makes things far easier.Say you have a block of code where a lot of different statements can cause an error. Like, say, loading resources. Say we have this code for loading things:void loadStuff() {  resourceMgr->loadResource(GUY1);  resourceMgr->loadResource(GUYTHATGETSKILLED);  resourceMgr->loadResource(EXPLOSION);  resourceMgr->loadResource(FLASHYTHING);  ...}  Any of those statements can cause an error (if the file isn''t found, if we''re out of memory, etc..). But we want to make things easy, we want error checking to be centralized, we don''t want to repeat a lot of code. Okay, so we can have a global ''encounteredError'' value, and we can write the function loadResource() so that if it encounters an error, it sets encounteredError appropriately. So that''s good- our error checking is all in one place.But here''s the annoying part- if loadResource() encounters an error, we want loadStuff() it to return immediately, we don''t want it to keep going because it''s probably just going to cause more errors. So how do we make sure loadStuff() returns immediately if theres an error?Here is the annoying way   to fix that problem: have loadResource() return a boolean, and rewrite loadStuff() like this:boolean loadStuff() {  if (!resourceMgr->loadResource(GUY1)) return false;  if (!resourceMgr->loadResource(GUYTHATGETSKILLED)) return false;  if (!resourceMgr->loadResource(EXPLOSION)) return false;  if (!resourceMgr->loadResource(FLASHYTHING)) return false;  ...}  Look at how annoying that is. And it can get even worse, when there are several places around the code that can fail, because in every single place   it can fail, we have to return a boolean and check the boolean to see if we should stop.That''s why exceptions make things much easier. If we just have loadResource() throw an exception on failure, then every function along the line will get stopped, until someone finally catches it. We don''t have to return booleans and check booleans every single damn time we call a function. ;  However, Exceptions SHOULDNT replace normal error checking, by their very nature they are exceptional events.As to when to use exceptions and when to use error checking, well it does come down to experiance to an extent.However, one area where the are VERY usefull is in object construction, because you cant return a value from a constructor, so if something critial goes wrong in the object setup, such as not being able to init winsock as i''ve got in a program, you can throw an exception and let the caller know that things have gone very wrong indeed and that they should clean up and deal with it.