Can I use an Else statement after a Try Catch?

I've written a simple function that's meant to check whether files are open. In it I've written a simple exception, but should the exception throw nothing, should I put an else statement after the catch expression?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*The function was made a template in order to
work with objects of ofstream, ifstream, and fstream type*/
template<typename f>
bool checkFileOpen(const f& file)
{
     try
     {
          if (!file.is_open())
          {
               throw 1;
          }
     }
     catch (int fileNum)
     {
          std::cout << "Error: File " << fileNum << " not found" << std::endl;
          system("pause");
          return false;
     }

     return true;
}


The Questions:

(1): If the try-catch block were to throw an exception, and the return false; command is reached, will the function keep running and end up returning true? Or will it stop there and carry on wherever it was called?

(2): Is it possible to put a simple else statement after the catch block, expecting that what was within the else statement would execute if no exception was thrown?
1) return false will cause the function to exit immediately.

2) No. The only place you could place an else statement is after line 11.

Topic archived. No new replies allowed.