Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Request: Exceptionhandling?  (Read 15093 times)

0 Members and 1 Guest are viewing this topic.

Haikarainen

  • Guest
Request: Exceptionhandling?
« on: July 03, 2011, 06:03:47 am »
Why not create a sf::Exception to make SFML-usage much more flexible and easier to debug?

Take sf::Image for example, if you try to load an image that doesn't exist to it using sf::Image::LoadFromFile();, all it does is print "Error: File blablabla". It should throw an exception instead, so you can handle your errors appropriate to your actual project.

Like if you have a custom error-log-management, you could do this:
Code: [Select]

try{
sf::Image Bleh;
Bleh.LoadFromFile("./data/bleh.jph");

sf::Image Bleh2;
Bleh.LoadFromFile("./data/bleh2.jph");
}catch(sf::Exception &e){
myErrorManager << e;
}


instead of:
Code: [Select]

sf::Image Bleh;
if(!Bleh.LoadFromFile("./data/bleh.jph")){
MyErrorManager << "Couldn't load bleh from bleh.jph, this error is hardcoded and not very flexible.";
}



sf::Image Bleh2;
if(!Bleh2.LoadFromFile("./data/bleh2.jph")){
MyErrorManager << "Couldn't load bleh2 from other file bleh2.jph, this error is hardcoded and not very flexible.";
}


I was gonna post this as an issue/feature/request on the bugtracker, but didn't know how to approach that since i never used something like that before :P

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Request: Exceptionhandling?
« Reply #1 on: July 03, 2011, 09:35:49 am »
Exceptions don't work well with shared libraries, that's why SFML doesn't use them. And they would be hard to translate to other languages such as C.
Laurent Gomila - SFML developer

Haikarainen

  • Guest
Request: Exceptionhandling?
« Reply #2 on: July 03, 2011, 09:56:07 am »
Quote from: "Laurent"
Exceptions don't work well with shared libraries, that's why SFML doesn't use them. And they would be hard to translate to other languages such as C.


Excuse my stupidity, but why wont they work well with shared libraries? You wouldn't have to translate them to other languages, just keep them to the most supported ones :)

If you ask me SFML would get alot cleaner, much more professional appeal etc if you'd implement exceptions.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Request: Exceptionhandling?
« Reply #3 on: July 03, 2011, 10:27:47 am »
Quote
Excuse my stupidity, but why wont they work well with shared libraries?

It's tricky to get exceptions that can cross shared libraries boundaries. You can read a lot about it on the internet ;)

Quote
You wouldn't have to translate them to other languages, just keep them to the most supported ones

I need to report errors in bindings, so I have to translate exceptions to something else that the target language supports ;)

Quote
If you ask me SFML would get alot cleaner, much more professional appeal etc if you'd implement exceptions.

I agree :(
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Request: Exceptionhandling?
« Reply #4 on: July 03, 2011, 10:34:40 am »
Wasn't a reason not to implement exceptions that these errors are not considered fatal? That is, when an image can't be loaded, just show a white sprite instead of terminating the application?

Anyway, one can easily encapsulate the SFML functions and provide exceptions on top of them. That's what I do in the Resources module of my library, by the way.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Request: Exceptionhandling?
« Reply #5 on: July 03, 2011, 02:47:56 pm »
Quote
Wasn't a reason not to implement exceptions that these errors are not considered fatal? That is, when an image can't be loaded, just show a white sprite instead of terminating the application?

It was a reason. But after realizing that error handling is currently a total mess in SFML, I think that exceptions would make things a lot cleaner and safer.
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Request: Exceptionhandling?
« Reply #6 on: July 03, 2011, 06:34:30 pm »
Well I hope it doesn't come as else I will have to remake rbSFML I think.

Though a better way to handle errors is not bad actually.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Request: Exceptionhandling?
« Reply #7 on: July 03, 2011, 06:53:06 pm »
Maybe a way to enable/disable exceptions? So that bindings can still work with the current implementation, and those who want to use exceptions do something like sf::Exception::SetEnabled(true).
Want to play movies in your SFML application? Check out sfeMovie!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Request: Exceptionhandling?
« Reply #8 on: July 03, 2011, 06:56:13 pm »
Quote
Maybe a way to enable/disable exceptions?

It's not just a technical detail, it means a complete redesign of functions -- especially removing error codes from return types, and possibly making something useful of them.
Laurent Gomila - SFML developer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Request: Exceptionhandling?
« Reply #9 on: July 03, 2011, 08:02:28 pm »
I wouldn't change anything as for the return codes, not to break current projects and bindings. But just throw exceptions, if enabled, where it should be thrown.

That way, you have exception support without breaking anything.
Want to play movies in your SFML application? Check out sfeMovie!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Request: Exceptionhandling?
« Reply #10 on: July 03, 2011, 08:37:48 pm »
It doesn't make sense to have return codes together with exceptions...
Laurent Gomila - SFML developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Request: Exceptionhandling?
« Reply #11 on: July 03, 2011, 09:08:52 pm »
A very (very!) long time ago I asked the same question. http://www.sfml-dev.org/forum-fr/viewtopic.php?p=3522&highlight=exception#3522

At that time you convinced me with your arguments; will these arguments convince you today too ?  :P
SFML / OS X developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Request: Exceptionhandling?
« Reply #12 on: July 03, 2011, 11:00:22 pm »
No ;)

These arguments are still valid, but there are also many drawbacks that I didn't see at this time.
Laurent Gomila - SFML developer

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Request: Exceptionhandling?
« Reply #13 on: July 09, 2011, 12:45:38 am »
I would suggest following the Boost.Filesystem approach to the dual exception/error-code interface problem.

Pseudo-code example:
Code: [Select]

namespace sf
{
    class ErrorCode
    {
        // impl..
    };
    class Exception
    {
        // impl...
    };

    //...

    void Devil::SetNumber( int param )
    {
        ErrorCode ec;
        SetNumber( param, ec );

        if ( ec )
        {
            throw Exception( ec );
        }
    }

    void Devil::SetNumber( int param, ErrorCode& ec )
    {
        if ( param != 666 )
        {
            ec = ErrorCode( EvilError );
            return;
        }
       
        m_Number = param;
    }
}   // namespace sf

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Request: Exceptionhandling?
« Reply #14 on: July 09, 2011, 09:25:28 am »
How does that solve the problem of exceptions across shared libraries boudaries? ;)
Laurent Gomila - SFML developer