• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
HappyCoder

Inline try/catch

19 posts in this topic

So I just wanted to share an idea I have been juggling around in my head and wondered what you thought of it, if there is a language that implements this, and if you would use it.

I call it inline try catch. It is like the blend between try/catch and the ternary ? operator. An example of how to use it.

//block try catch
string result = null; 
try
{
    result = bar.getStringValue();
}
catch (BarException)
{
    result = "Default string";
}

// inline try catch
string result = try bar.getStringValue() catch(BarExeption) "Default string";
What I like about this is the try catch block returns a value, this would reduce branch complexity but at the cost of having longer line length. I could see the inline try being quite ugly if care isn't taken to keep it simple.

Any thoughts?
0

Share this post


Link to post
Share on other sites

This looks like a convenient syntax to have, but what do you mean it would reduce branch complexity? I imagine a compiler would translate it into the same try catch block it represents.

0

Share this post


Link to post
Share on other sites

That's an example of expection handling (no, that's not a typo).

 

While this certainly "works", most people will consider it an abuse of the language. Also, it will certainly not reduce branch complexity, on the contrary. It will have the same branch complexity (only you don't see it!) plus RTTI overhead plus stack unwinding overhead.

 

Personally, I would prefer bar.getStringValue() to return a default string if it can't provide anything better. This also has no branches (only hidden inside the class, but not visible to the user), and it feels "cleaner". But of course that's a bit of a matter of taste, too.

Edited by samoth
1

Share this post


Link to post
Share on other sites

That's an example of expection handling (no, that's not a typo).
 
While this certainly "works", most people will consider it an abuse of the language. Also, it will certainly not reduce branch complexity, on the contrary. It will have the same branch complexity (only you don't see it!) plus RTTI overhead plus stack unwinding overhead.
 
Personally, I would prefer bar.getStringValue() to return a default string if it can't provide anything better. This also has no branches (only hidden inside the class, but not visible to the user), and it feels "cleaner". But of course that's a bit of a matter of taste, too.


I realize that the generated code would be similar if not identical. It would reduce branch complexity of the source code for somebody who has to read and maintain it. And yes, in this particular case try catch is probably not the best way to handle having a default string. There is probably a better example to be used here. Edited by HappyCoder
0

Share this post


Link to post
Share on other sites

I agree that it could be convenient in some situations, but it can be implemented without changes to some languages. Here's a quick example in C#:

 

(keep in mind that these examples are contrived, and in this situation it'd be much cleaner to do Console.WriteLine(t == null ? "" : (t.ToString() ?? ""))

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Thing t = null;

            // t is null so the fail delegate is called
            Console.WriteLine(InlineTryCatch(()=> t.ToString(), ()=> "Fail 1 (OK)"));

            // t is null, but there's no fail so default(T) is returned
            Console.WriteLine(InlineTryCatch(() => t.ToString(), null));

            // t is null and a NullReferenceException is thrown. That's the one we're looking for, so fail delegate is called
            Console.WriteLine(InlineTryCatch<string, NullReferenceException>(() => t.ToString(), (ex) => "Fail 2 (OK)"));

            try
            {
                // t is null, but the exception thrown doesn't match. because it's not a matching exception
                // the exception is rethrown
                Console.WriteLine(InlineTryCatch<string, IndexOutOfRangeException>(() => t.ToString(), (ex) => "Fail 3 (OK)"));
            }
            catch
            {
                Console.WriteLine("Exception 1");
            }

            // no exception so the success delegate is called
            t = new Thing();
            Console.WriteLine(InlineTryCatch(() => t.ToString(), () => "fail 4"));
        }

        class Thing
        {
            public override string ToString()
            {
                return "Win!";
            }
        }

        static T InlineTryCatch<T, U>(Func<T> func, Func<U, T> fail) where U : Exception
        {
            try
            {
                return func();
            }
            catch (U ex)
            {
                if (fail != null)
                {
                    return fail(ex);
                }
                else
                {
                    throw ex;
                }
            }
        }

        static T InlineTryCatch<T>(Func<T> func, Func<T> fail)
        {
            return InlineTryCatch<T, Exception>(func, (ex) => fail == null ? default(T) : fail());
        }
    }
}
 
Edited by smr
0

Share this post


Link to post
Share on other sites

Many languages have a null coalescing operator.

 

Basically it says "If this value is non-null, use it.  If it is null, use this instead".

 

And used along with the ternery if operator it pretty much covers all the cases when you'd want to do the inline try/catch.

 

t == null ? "" : (t.ToString() ?? "")

0

Share this post


Link to post
Share on other sites

Many dynamic languages allow you to write code like the following:

result = (bar.getStringValue() or "Default string")
result = bar.getStringValue() || "Default string"

In some, statement are also expression, and the last evaluated value is the overall result: 

def foo()
    if rand() < 0.5
        return "Yay!"
    end
    raise "Oh no"
end
 
x = begin foo() rescue "Default string" end
Edited by rip-off
0

Share this post


Link to post
Share on other sites

In many languages (Java in particular - I hate checked exceptions) I feel exception handling is somewhat lackluster.
 
One thing I dislike is having to push a variable declaration up, outside of a try-catch block and assigning null to it, like in HappyCoder's original example (and for value types there might not be a sensible initial value to give it).
 
Perhaps try-catch shouldn't define a scope at all? Initially that seems unworkable if you consider that an exception might cause some vars to not get declared at all. However most catch-blocks don't really handle the error, they just wrap more detail around an exception and re-throw, perhaps with some logging thrown in and occasionally they might also do some cleanup, but either way the stack is about to unwind.

 

Given this, maybe there should be a separate syntax for that use-case: An all-or-nothing try-catch. If it fails you are given a chance to intercept it but ultimately the containing function will exit after that. If it succeeds then any vars declared in the try-block are still kept in-scope for subsequent code to use.

 

I'd be more interested in seeing something like that than an inline try-catch as proposed here.

 

Lacking that you end up either lumping code that didn't really need to be in the try-catch into the try-block. Or, if you only need one variable out, you end up extracting the try-catch into it's own function.

Edited by dmatter
1

Share this post


Link to post
Share on other sites

It sounds like a automatic-rethrow exception.

try{


}
catch(AllOrNothing by_value){
 // your interception code
 } 

// the magic class (this is just a draft

class AllOrNothing: public virtual std::exception{

public: 

    virtual ~AllOrNothing(){
        if(!flag)
            throw this->clone(~flag);
    }

};


when the catch block goes out of scope the argument is destroyed (if passed by value) and it throws. Of course it sets a flag when cloning so that it rethrows only once.

Edited by DemonRad
-1

Share this post


Link to post
Share on other sites

But you never ever want to catch anything by value (slicing) - assuming C++ of course. You always want to throw by value and catch by reference, or else bad things may happen.

0

Share this post


Link to post
Share on other sites

You already have stack space for that. of course assuming the exception do not allocate extra memory in copy ctor...

Edited by DemonRad
0

Share this post


Link to post
Share on other sites

Note that throwing an exception from a destructor is also super bad mojo in C++. Basically any exception guarantees go out the window if you have destructors that throw. It's sufficiently bad that C++11 gave all destructors an implicit noexcept exception specification, meaning that an exception from a destructor will immediately call terminate() unless you specifically enable exceptions for the destructor for the class or one of the subobjects of that class contains a destructor which has exception enabled destructors.

1

Share this post


Link to post
Share on other sites

Note that throwing an exception from a destructor is also super bad mojo in C++. Basically any exception guarantees go out the window if you have destructors that throw. It's sufficiently bad that C++11 gave all destructors an implicit noexcept exception specification, meaning that an exception from a destructor will immediately call terminate() unless you specifically enable exceptions for the destructor for the class or one of the subobjects of that class contains a destructor which has exception enabled destructors.

 

Actually I find this one of the more valuable additions to C++11, because if your destructor throws, something is seriously fucked up. At least, usually... there may be singular cases where it may be allowable and desirable, but I must admit I don't see them. I do see the problems, though.

 

Either you have a "conditional destructor" with some (hopefully bulletproof) logic that prevents it from throwing while being unwound, then your class behaves differently although the expectation would be that it works the same. You now need a separate way of detecting the error condition, or you must simply ignore the error. Either way, this stinks.

 

Or, you are unaware of what happens when you throw from a destructor during unwind, and do it anyway. This is even worse, in two respects (first, because you don't know the implications, and second because it actually happens, and the program is now in an undefined, unrecoverable state). And, what's worst, now you don't even know what happened, or where.

 

So, for that this case, crashing hard by terminating the program is a good thing. It forces the programmer to address the issue. Actually, the mere presence of the throw keyword inside a destructor should preferrably generate a build error, if you ask me.

Edited by samoth
1

Share this post


Link to post
Share on other sites
One problem I see with inlineing try catch like that is that it makes it too easy to use. Throwing and catching an exception is an expensive operation, and it's good when the code reflects that complexity. You don't want to encourage non exceptional cases to throw exceptions for the sake of convenient syntax.
2

Share this post


Link to post
Share on other sites

Ruby allows this with a simpler syntax.

 

'Normal' exception handling:

begin
  10 / 0
rescue
  puts "Please do not divide by zero."
ensure #the 'ensure' section will always execute when the block exits, regardless of exceptions
  puts "Moving right along..."
end

'Inline' exception handling:

10 / 0 rescue puts "Please do not divide by zero."
puts "Moving right along..."

In the 'inline' case, the handler extends to the end of the line. You can have multiple statements if you use semicolons to separate them.

Edited by Khatharr
0

Share this post


Link to post
Share on other sites

Note that throwing an exception from a destructor is also super bad mojo in C++. Basically any exception guarantees go out the window if you have destructors that throw. It's sufficiently bad that C++11 gave all destructors an implicit noexcept exception specification, meaning that an exception from a destructor will immediately call terminate() unless you specifically enable exceptions for the destructor for the class or one of the subobjects of that class contains a destructor which has exception enabled destructors.


Since all guarantees go out of the window if you throw in the destructor it is a *good thing* that destructors are marked noexcept, that way you have a guarantee that terminate is called, which I'll take over all kinds of undefined behavior (which throwing in the destructor will cause when used in conjunction with pretty much any library class or template which doesn't expect that... which includes all standard containers) any day.
0

Share this post


Link to post
Share on other sites

Since all guarantees go out of the window if you throw in the destructor it is a *good thing* that destructors are marked noexcept, that way you have a guarantee that terminate is called, which I'll take over all kinds of undefined behavior (which throwing in the destructor will cause when used in conjunction with pretty much any library class or template which doesn't expect that... which includes all standard containers) any day.

 

 

I don't see where you get the idea that I'm saying that the implicit noexcept specification is a not a good thing. "It's sufficiently bad" as a connecting clause here means that the previously mentioned is so very bad that the part after the "sufficiently bad" clause is a step taken to help mitigate the badness. In other words a good thing. Another example: there are a lot of rapes in this area. It's sufficiently bad that the city has doubled the number of police patrols in the area. Rapes = bad thing. More police patrols = good thing that is done to reduce the problems of the bad thing.

0

Share this post


Link to post
Share on other sites

 

Since all guarantees go out of the window if you throw in the destructor it is a *good thing* that destructors are marked noexcept, that way you have a guarantee that terminate is called, which I'll take over all kinds of undefined behavior (which throwing in the destructor will cause when used in conjunction with pretty much any library class or template which doesn't expect that... which includes all standard containers) any day.

 

 

I don't see where you get the idea that I'm saying that the implicit noexcept specification is a not a good thing. "It's sufficiently bad" as a connecting clause here means that the previously mentioned is so very bad that the part after the "sufficiently bad" clause is a step taken to help mitigate the badness. In other words a good thing. Another example: there are a lot of rapes in this area. It's sufficiently bad that the city has doubled the number of police patrols in the area. Rapes = bad thing. More police patrols = good thing that is done to reduce the problems of the bad thing.

 

Ah, sorry, I basically read it as in "It's bad enough (sufficiently bad) that they did this, but even worse...". And going by samoth's post I'm not the only one to have understood it that way.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0