-1

If I have the following code

try{
   // Some code that might throw a system_error
   -- snip --
} catch(const std::system_error& ex) {
  if(ex.code()!=std::errc::permission_denied){
    // Not a permission denided error
   throw;
  }
// Recover from a permission denied
--snip---
}

can someone explain the function of throw? Is it correct that the try-catch statement will re-execute until it finds a permission_denied error?

user2820579
  • 3,261
  • 7
  • 30
  • 45
  • 2
    Please invest in [a good books or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). This is really a rather basic thing that should be explained in any decent beginners book, class or tutorial. – Some programmer dude Apr 18 '23 at 19:54
  • No, this is the example from my book and I don't understand it. – user2820579 Apr 18 '23 at 19:55
  • Is it really a beginners C++ book? What have it said about exceptions and the `throw` statement before this example? It should have covered what the `throw` statement does, unless it's a intermediate book that assumes familiarity with basic C++? What *is* the book you're reading? – Some programmer dude Apr 18 '23 at 19:57
  • And please excuse me sounding rude, it's not really my intention. I'm not criticizing you, only your book. (Well, maybe you a *little* if you skipped or skimmed over the chapters and sections that taught the `throw` statement... ;)) – Some programmer dude Apr 18 '23 at 19:59
  • Np, it's C++ crash course by Josh Lospinoso. I'm reading the book per-page. I used to know fortran 90, so many "low level" programming seem familiar, but others definitely not. – user2820579 Apr 18 '23 at 20:00
  • Back when I was reading a lot of C++ texts looking for a good one for new hires I don't think I found any worthwhile "Learn C++ in a hurry" books. C++ is a hard language to rush. – user4581301 Apr 18 '23 at 20:23
  • I would be very interested if you have an in-depth, yet understandable, reference. I don't mind spending time learning it. – user2820579 Apr 18 '23 at 20:27
  • 2
    [Here's the big list of recommended books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Since you already have a programming background you can skip most of *Programming: Principles and Practice Using C++* and maybe graduate straight on to *A Tour of C++*, but be warned *Tour* is extremely terse. Here's a older version of *Tour* you can browse to see if it's blowing past stuff you wanted better-explained. If it isn't, the third edition of the printed book should bring you right up to date with cutting edge C++. – user4581301 Apr 18 '23 at 20:52
  • At this time there are no truly good middle-ground books covering C++ from 2017 on. I mean to try out *Programming: Principles and Practice Using C++* and *tour* for the update to C++20 next time I get a C++ hire. – user4581301 Apr 18 '23 at 20:55

1 Answers1

3

throw; will re-throw the caught exception, allowing it to be caught and handled by something higher up the call stack (or escape from main and terminate the program).

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • If I don't get a permission_denied, where does the program execution go in this code? – user2820579 Apr 18 '23 at 19:59
  • 3
    @user2820579 Up the call-stack to the next higher `catch` clause. Or to the termination of the program if no other suitable `catch` clause is found. – Some programmer dude Apr 18 '23 at 20:01
  • @user2820579 what do you mean by "does it faithfully ends?"? – Jesper Juhl Apr 18 '23 at 20:07
  • Ahg sorry! I got confused by your statement "or escape from main and terminate the program". So what you mean is that if another handler gets a system_error and know what to do with it, the program may actually terminate, right? – user2820579 Apr 18 '23 at 20:08
  • 2
    @user2820579 If another `catch` handler catches the exception it can do whatever it wants with it. But if you have no `catch` that catches the exception at all anywhere up the stack (whether thrown the first time or re-thrown), then the program is immediately terminated on the (re-)throw by an implicit call to [`std::terminate`](https://en.cppreference.com/w/cpp/error/terminate), potentially (but not necessarily) after stack unwinding called all destructors of automatic storage duration variables of the stack at the time of the (re-)throw. – user17732522 Apr 18 '23 at 20:17