[Solved] How do I prevent exceptions from closing the program?

Problems building or running the engine, queries about how to use features etc.
Post Reply
Tikoucas
Gnoblar
Posts: 7
Joined: Fri Dec 28, 2018 7:52 pm
x 1

[Solved] How do I prevent exceptions from closing the program?

Post by Tikoucas » Tue Jan 08, 2019 6:47 pm

Ogre Version: :1.7.2 SDK:
Operating System: :Win XP SP3:

Hello, again more newbie questions.
I have my map editor which load assets (zip files) from users, you can expect all bad things: Duplicated/missing particles/meshes/materials invalid scripts, etc.
When something goes wrong i'd like to log/display the error and maybe load a default mesh/object and allow the program to continue running.

I tried a simple try/catch around some code that will cause crash and the catch is never being executed, how I do handle errors in Ogre?
Code at CreateScene()

Code: Select all

    try
    {
        printf("let's crash this!...  \r\n");
        SceneNode *BrownSN = mSceneMgr->getRootSceneNode()->createChildSceneNode("BrownSN");
        Entity *HeadA = mSceneMgr->createEntity("Head2", "Head2.mesh");
        BrownSN->attachObject(HeadA);
        Entity *HeadB = mSceneMgr->createEntity("Head2", "Head2.mesh");  //this will crash it
        BrownSN->attachObject(HeadB);
    }
    catch (Exception& e)
    {
        printf("Crashed as expected\r\n");
        fprintf(stderr, "An exception has occurred: %s\n",e.getFullDescription().c_str());
        system("pause");
    }
Output:

Code: Select all

let's crash this!...
Mesh: Loading Head2.mesh.
Skeleton: Loading Head2-Armature2.skeleton
Texture: headTex1.jpg: Loading 1 faces(PF_R8G8B8,512x512x1) with  hardware gener
ated mipmaps from Image. Internal format is PF_X8R8G8B8,512x512x1.
Texture: teethTex.jpg: Loading 1 faces(PF_R8G8B8,128x128x1) with  hardware gener
ated mipmaps from Image. Internal format is PF_X8R8G8B8,128x128x1.
Texture: eyeTex.jpg: Loading 1 faces(PF_R8G8B8,512x512x1) with  hardware generat
ed mipmaps from Image. Internal format is PF_X8R8G8B8,512x512x1.
WARNING: the mesh 'Head2.mesh' includes vertices with more than 4 bone assignmen
ts. The lowest weighted assignments beyond this limit have been removed, so your
 animation may look slightly different. To eliminate this, reduce the number of
bone assignments per vertex on your mesh to 4.
terminate called after throwing an instance of 'Ogre::ItemIdentityException'
  what():  OGRE EXCEPTION(4:ItemIdentityException): An object of type 'Entity' w
ith name 'Head2' already exists. in SceneManager::createMovableObject at ../../.
./../../OgreMain/src/OgreSceneManager.cpp (line 6484)

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3)   execution time : 9.906 s
Press any key to continue.

As you can see, the catch here is never being executed. Any ideas?

Another different question, how do I make Ogre override/ignore duplicated particle scripts rather than throwing an exception and closing when I load a resource group?
Any tutorial with good practices for error handling in Ogre?

Edit: Forgot to create a child node, yet the error persist.
Last edited by Tikoucas on Tue Jan 15, 2019 4:15 am, edited 1 time in total.
0 x

User avatar
saintnick
Halfling
Posts: 51
Joined: Tue Nov 27, 2018 1:41 am
x 2

Re: How do I prevent exceptions from closing the program?

Post by saintnick » Sun Jan 13, 2019 6:07 pm

My C++ reference calls for a const in the statement.

Code: Select all

catch (const std::exception& e)
0 x

Tikoucas
Gnoblar
Posts: 7
Joined: Fri Dec 28, 2018 7:52 pm
x 1

Re: How do I prevent exceptions from closing the program?

Post by Tikoucas » Mon Jan 14, 2019 8:07 pm

Thank saintnick, I tried that but I'm still unable to catch the exception. The debugger shows clearly the catch is never happening.
I reduced the code to this, and I don't have a clue what I'm doing wrong.

Code: Select all

#include "ExampleApplication.h"

class TutorialApplication : public ExampleApplication
{
protected:
public:
    TutorialApplication()
    {
    }

    ~TutorialApplication()
    {
    }
protected:
    void createScene(void)
    {
        try
        {
            printf("Try \r\n");
            //throw 20;
            SceneNode *BrownSN = mSceneMgr->getRootSceneNode()->createChildSceneNode("BrownSN");
            Entity *HeadA = mSceneMgr->createEntity( "Robot2", "robot.mesh" );
            BrownSN->attachObject(HeadA);
            Entity *HeadB = mSceneMgr->createEntity( "Robot2", "robot.mesh" );
            BrownSN->attachObject(HeadB);
        }
        catch (const std::exception& e)
        {
            printf("catch (const std::exception& e)\r\n");
            system("pause");
        }
        catch (...)
        {
            printf("catch (...)\r\n");
            system("pause");
        }

    }
};

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char **argv)
#endif
{
    // Create application object
    TutorialApplication app;

    try
    {
        app.go();
    }
    catch ( Exception& e )
    {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
        MessageBox( NULL, e.what(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
        fprintf(stderr, "An exception has occurred: %s\n",
                e.what());
#endif
    }

    return 0;
}
Output:

Code: Select all

Finished parsing scripts for resource group Popular
Try
Mesh: Loading robot.mesh.
Skeleton: Loading robot.skeleton
Texture: r2skin.jpg: Loading 1 faces(PF_R8G8B8,512x512x1) with  hardware generat
ed mipmaps from Image. Internal format is PF_X8R8G8B8,512x512x1.
terminate called after throwing an instance of 'Ogre::ItemIdentityException'
  what():  OGRE EXCEPTION(4:ItemIdentityException): An object of type 'Entity' w
ith name 'Robot2' already exists. in SceneManager::createMovableObject at ../../
../../../OgreMain/src/OgreSceneManager.cpp (line 6484)

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3)   execution time : 13.422 s
Press any key to continue.
When I uncomment the line
//throw 20;
I do actually get a exception catch, and after reading a lot of exception tutorials for c++ It seems like all my code is right.
0 x

User avatar
EricB
Gnome
Posts: 314
Joined: Fri Apr 09, 2010 5:28 am
Location: Florida
x 103
Contact:

Re: How do I prevent exceptions from closing the program?

Post by EricB » Tue Jan 15, 2019 1:07 am

Code looks fine to me (although way too many try/catches). Check your compiler's Exception Handling settings. For Visual Studios: https://docs.microsoft.com/en-us/cpp/bu ... ew=vs-2017
1 x
Image

paroj
OGRE Team Member
OGRE Team Member
Posts: 837
Joined: Sun Mar 30, 2014 2:51 pm
x 148
Contact:

Re: How do I prevent exceptions from closing the program?

Post by paroj » Tue Jan 15, 2019 1:16 am

EricB wrote:
Tue Jan 15, 2019 1:07 am
Code looks fine to me (although way too many try/catches). Check your compiler's Exception Handling settings. For Visual Studios: https://docs.microsoft.com/en-us/cpp/bu ... ew=vs-2017
yeah, that also looks like an ABI issue to me. If you are using the prebuild SDK from the website, your code should be compiled with "/EHsc".
1 x

Tikoucas
Gnoblar
Posts: 7
Joined: Fri Dec 28, 2018 7:52 pm
x 1

Re: How do I prevent exceptions from closing the program?

Post by Tikoucas » Tue Jan 15, 2019 4:14 am

@EricB & @paroj
Thank you so much, I never thought it was a compiler issue.
Since you mentioned about compiler and ABI, as soon I changed the compiler version I got the exception working and all fine.
I wasted a week believing there was something with my code.
I was using mingw32 3.4.5 and I got my application working perfectly fine even with a hundred warnings using mingw32 4.5.0
I tried mingw32 8.2.0 but I got lot of compile errors.
And Yes, I was using a SDK verion.
0 x

Post Reply