c++ - Reusing variables declared on Try-Catch -
i using try-catch block one:
try { texture heightmaptexture = texture("snow0101_7_m.jpg"); } catch (textureloadexception exc) { std::cout << exc.what() << std::endl; }
the thing need reuse variable heightmaptexture further in program. realized can't because of scope. should put rest of program inside scope? me doesn't make sense.
i can't declare variable outside of scope because have initialize it. has constructor receives string input.
what best solution?
i realize use pointer, trying avoid (i not @ preventing memory leaks).
edit: sorry, declaring variable class heightmap, wrong!, texture object. problem same.
you'd want logic inside 1 try/catch context. presumably if loading of texture fails, after fail also?
in case might able express logic more neatly as:
try { heightmap heightmaptexture = texture("snow0101_7_m.jpg"); // work here // if fails fatally, throw // need store height map in object? my_object.give_heightmap(std::move(heightmaptexture)); } // report failures here catch (textureloadexception& exc) { // note: reference std::cout << exc.what() << std::endl; } catch (otherreason& e) { // report reason } catch(exception& e) { // other failures }
a way think exception handling exception object explanation being given process or sequence failing, rather each exception being error code 1 operation.
Comments
Post a Comment