How to instantiate an object in a try block, but keep it in scope?

Pages: 12
closed account (Ezyq4iN6)
@dhayden

Your code from [Jun 3, 2019 at 5:22pm] works for me.
I do not get an error like you list in the comment on line 15.


@Enoizat

I did not mean the question to be theoretical code. I mean it to
be a question about how to properly deal with errors inside of
class constructors, as this is something I have run into with
multiple classes I have written. I'm usually lazy and just take
all of the error handling out (bad I know). A real world example
is me initializing SDL. I will include my code beneath this. I'll
omit all of the extra class functions, since they have nothing to do
with my question.

I was going to test your code, but apparently #include optional
isn't working for me. I am using Window 10, Code::Blocks, mingw,
and tried updating my compiler flags to C++ 17, but it won't
recognize it. Seems like a good solution though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// main.cpp
#include "My_SDL.hpp"
#include <iostream>
int main(int argc, char * argv[])
{
  try
  {My_SDL sample;}
  catch (int x)
  {
    if (x == -1)
      return 1;
    else
      std::cout << "You shouldn't see this.";
  }
  return 0;
}


// My_SDL.hpp
#ifndef MY_SDL_HPP
#define MY_SDL_HPP
#include <SDL.h>
class My_SDL
{
  public:
    My_SDL();
    ~My_SDL();
};
#endif

// My_SDL.cpp
#include "My_SDL.hpp"
#include <iostream>
My_SDL::My_SDL()
{
  if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
  {
    std::cout << "ERROR: SDL failed to initialize!";
    throw -1;
  }
}
My_SDL::~My_SDL()
{
  SDL_Quit();
}


@Niccolo

The structure I use for my throwing usually goes something like this.

1
2
3
4
5
6
7
8
9
if (!SDL_Initialize())
{
   std::cout << "ERROR: Failed to initialize SDL!" << "\n";
   throw -1;
}
else
{
  // Rest of the program code.
}


My basic thought process is this. When I try to initialize something
like SDL or OpenGL, the function will return a certain value on failure.
I use a nested if/else structure to check for the failure. If I see a
failure then I output some meaningful text and throw a generic integer.
The constructor will then exit, else it will continue in the else
block, possibly containing more blocks if more testing is needed. Upon
return from the constructor, if something was thrown, then the catch
simply takes it and exits main. Since the problem is described in each
if statement, I figured it is ok to throw a generic integer, as the
problem has already been described from output via the class.

And I agree about having it only for serious errors. I am only using
it for when a key part of the program fails. If SDL doesn't initialize,
I won't be able to do anything, so I consider that a bad enough error
to quit. I'm not sure what I could do to fix the problem. Maybe I could
loop a few times on error, trying again, and only quit if I fail 5 tries,
or something like that.

@TheIdeasMan

Almost all of the code I see for SDL involves some kind of if statement to
check for errors, then it exits the program. This is when the SDL is done
in main, however I move it to a class to modularize things, so I'm trying
to find a way to gracefully exit on fail.
RAII simply means that an object cleans up after itself when it goes out of scope
Doesn't it also mean that if you can't allocate the resource then you throw an exception? Somebody has to catch it or the program terminates.

My apologies to OP for hijacking the thread.
closed account (Ezyq4iN6)
No apology needed. I think it is interesting to read all of the comments.
Topic archived. No new replies allowed.
Pages: 12