-4

My compiler gives me an error for the following code:

#include <iostream>
#include <stdexcept>

using namespace std;


void test()
{
     throw runtime_error("Error");
}

int main()
{
     try
     {
          test();
     }

     for (int i = 0; i < 10; i++)
     {

     }

     catch (exception& e)
     {
          cout << e.what();
     }
}

It says "error: expected 'catch' before '(' token", and it's referring to the '(' in the for loop initialization.

Do I have to write the catch block immediately after the try block? I thought that if an error is thrown in the try block, the program will bubble out until it finds an appropriate catch block. Why doesn't that apply here?

  • 4
    Yes. Question closed. – SergeyA Mar 16 '18 at 18:28
  • 1
    yes, `try` and `catch` are not independent constructs. They constitute a single compound expression – Srini Mar 16 '18 at 18:28
  • 1
    I'm voting to close this question as off-topic because the answer is a single word 'Yes'. – SergeyA Mar 16 '18 at 18:29
  • It's called try-catch for a reason. The one is followed by the other. – NathanOliver Mar 16 '18 at 18:29
  • 1
    @SergeyA That's not a reason to close a question. It's on topic an answerable so it shouldn't be closed. – NathanOliver Mar 16 '18 at 18:30
  • 2
    @NathanOliver it is hard to tell, to be honest. This is basic language feature. Does function always have to have a return type specified (or void)? Is `new` keyword always spelled like this, or can you also spell it `knew`? I feel like questions like that add little value. – SergeyA Mar 16 '18 at 18:32
  • 3
    @InertialIgnorance SO is not a tutorial service. And such a question would be answered in any [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Did you try reading any? – Algirdas Preidžius Mar 16 '18 at 18:34
  • 3
    @InertialIgnorance you can ask any question you want, which you just did. You can even get an answer, which you also did. But questions like yours do not add value to the site in general, because they are not likely to be of much help to other people. This is why simple textbook questions are often frowned upon. – SergeyA Mar 16 '18 at 18:37
  • 1
    @InertialIgnorance In addition to what already has been said: books typically are structured into chapters. So, what's stopping you from, at the very least, reading a chapter you are interested about, so you would understand the basic syntax of the language, before asking about it? – Algirdas Preidžius Mar 16 '18 at 18:42
  • @InertialIgnorance: If you had to pay a fair price for our time teaching you the basics, a bok would be a much cheaper way to learn the language. **Plus** you would learn the language properly. You won't by watching obscure online-tutorials or youtube videos and asking aprticular questions. That will not give you the whole image you need. So, according to your last questions: do **yourself** (not even talking about the community) a favour and eventually get a good textbook. – too honest for this site Mar 16 '18 at 19:07

2 Answers2

7

Yes it does. And by yes, I mean it does.


To be explicit, as some people seem to be confused by the above brief answer:

A catch block does need to be written immediately after a try block.

There is a secondary question:

I thought that if an error is thrown in the try block, the program will bubble out until it finds an appropriate catch block. Why doesn't that apply here?

Because you can only write a catch block immediately after a try block. Anything else is an ill formed program.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 1
    And by does, you mean it yes? – SergeyA Mar 16 '18 at 18:29
  • 2
    @SergeyA Yes, more or less, well more more than less, as less is not 30 while more can be 30, and thirty is the least that I can yes. – Yakk - Adam Nevraumont Mar 16 '18 at 18:31
  • @Yakk Care to explain? – Inertial Ignorance Mar 16 '18 at 18:31
  • 4
    @InertialIgnorance The answer is yes. The explaination is that yes means it (catch block) does (need to be written immediately after a try block). So "Yes, yes it does" is a complete answer to your question. The rest of your post is based off the mistaken assumption you can write it somewhere else. As you cannot, there is no "what should happen". There could be another language where catch blocks are not forced to be adjacent to try blocks, but that isn't C++, and its behavior is not something I'm qualified to talk about. – Yakk - Adam Nevraumont Mar 16 '18 at 18:32
  • @Yakk Is there a conceptual reason, or is this simply a rule of the language. – Inertial Ignorance Mar 16 '18 at 18:34
  • 1
    @InertialIgnorance I honestly don't understand that question. I mean, I could blather on about it, and give opinions and the like, but opinion based questions are off-topic on SO. If you are asking why the langauge construct is like that, that is lost to time (possibly pre-90s!); so you'd have to look at comparative language design and see if any languages handle catch blocks differently and which ones existed prior to C++ and which did not. Avoiding just spewing opinions here would be hard, and it would have to be a very well crafted specific question to talk about it on SO. – Yakk - Adam Nevraumont Mar 16 '18 at 18:37
  • 1
    @InertialIgnorance • there are C++ constructs that have a strict pattern: `if-else` (the else part being optional), `do-while`, `switch-case-default`, and `try-catch`. The `try-catch` code structure isn't something unique in this regard. – Eljay Mar 16 '18 at 18:46
  • 1
    @InertialIgnorance It is a hard rule of the language, but for conceptual reasons. This is how try/catch and exception handling works virtually everywhere. You can't separate your `try` and its `catch` by an arbitrary bunch of code, the catch is *part* of the try. Exceptions raised within a given `try` block are first handled by that `try` block's `catch`, and then they bubble up through **containing** `try`/`catch`s. Exceptions don't move linearly down through the literal lines of code in a method looking for a subsequent `catch`. – user229044 Mar 16 '18 at 18:47
  • @meager Makes sense. I didn't know try and catch are actually one construct. – Inertial Ignorance Mar 16 '18 at 18:48
0

it should always similar to this pseudo code: reference

try {
    risky code block
}
catch (ExceptionClassName exceptionObjectName) {
    code to resolve problem
}
Roushan
  • 4,074
  • 3
  • 21
  • 38