Best way to abuse exceptions

Pages: 12
Sep 2, 2014 at 1:39am
Just thought I'd shared something amusing in Java.
https://gist.github.com/LB--/a5dfb3a47c155ac7690e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//For best effect, pipe stderr to a file
public class InfiniteException extends Throwable
{
	public static void main(String[] args) throws Throwable
	{
		throw new InfiniteException();
	}

	@Override
	public Throwable getCause()
	{
		return new InfiniteException();
	}
}
I guess it's good that the implementation used a recursive approach or this really would be infinite! (Note: it magically detects if you return this; instead)
Last edited on Sep 2, 2014 at 1:46am
Sep 2, 2014 at 7:09pm
Is it really an abuse if nothing is actually accomplished?
Sep 2, 2014 at 8:59pm
Pretend this was used in place of System.exit()
Sep 2, 2014 at 9:38pm
Do you mean you actually used this in code that's meant to do something useful?
Sep 2, 2014 at 9:49pm
No, it's just a though experiment.
Sep 2, 2014 at 10:13pm
Then why not just
1
2
3
4
5
6
7
8
9
//deep in the call stack
throw new ExitException();

/*...*/ main(/*...*/){
    try{
        dummy_main(/*...*/);
    }catch (ExitException){
    }
}
That's actually what Boost.coroutine does to give objects on the "fake" stack a chance to clean up when a coroutine object is destructed before the coroutine entry point has had a chance to return.
Sep 3, 2014 at 3:52am
helios wrote:
...Boost.coroutine does [that] to give objects on the "fake" stack a chance to clean up when a coroutine object is destructed before the coroutine entry point has had a chance to return.


Some would call that... clever.
Last edited on Sep 3, 2014 at 3:53am
Sep 3, 2014 at 3:59am
There isn't really any other way to unwind the stack while calling destructors.
Sep 3, 2014 at 4:01am
Must be hard working in languages without RAII.
Sep 3, 2014 at 1:32pm
It's pretty amazing what you can do with Boost. There's a guy who used Boost.MPL to introduce a "code feature" concept that ensures e.g. functions marked as thread-safe can only call other functions marked as thread-safe.

http://www.artima.com/cppsource/codefeatures.html

It doesn't actually enforce thread-safety, though; they are just tag structures and they could be wrong.
Sep 3, 2014 at 1:42pm
@chrinsname: you can do that with std::tuple, it's just a design pattern and there's no real enforcement. Still clever though :)
Sep 3, 2014 at 1:55pm
Hey, you're right, you could use std::tuple (or boost::tuple in this case, since the article was written in 2008). Perhaps boost::mpl::vector has some other features.

It would be great to have a language feature that could enforce thread-and-exception-safety. I would also like the const specifier to be applicable to static-and-non-member functions (or perhaps a "pure" specifier [gcc has __attribute__((pure)) already]) which enforces that functions do not modify global state. It would make writing safe C++ functions more easy.
Sep 3, 2014 at 1:57pm
I have always wanted more -correctness in C++ than just const-correctness. Const-correctness alone is amazing and I am always missing it in other languages, but I frequently want to enforce other requirements and control which are convertible to others and how. Maybe in the far future when we get past Concepts Lite we can have some fun.
Sep 3, 2014 at 2:22pm
Haskell is good for that. In order to do anything with side-effects you have to use a monad, and it is enforced by the compiler e.g. if your function doesn't use the IO monad it can't use I/O, and it can't call any functions that do (unless you use the liftIO function, which lets you transform the IO monad to a different one).
Sep 3, 2014 at 2:32pm
I suppose that in a way, abstraction is really what we're doing, except that instead of abstracting using classes and objects, we're abstracting using functions and contracts. Am I right in thinking that? Or is it something else since, for example, const-correctness is viral?
Sep 3, 2014 at 2:44pm
Design-by-contract is usually not enforced by the compiler (although D does), what we're doing is like an abstracted but enforced version of DbC.
Sep 4, 2014 at 7:38am

I have always wanted more -correctness in C++ than just const-correctness.


C++ doesn't have const-correctness. It has only half of it (meaning - you're not allowed to change the object but you're given no guarantee it cannot be changed by something else). Rust has full const-correctness.
Last edited on Sep 4, 2014 at 7:38am
Sep 4, 2014 at 12:18pm
Const-correctness is read-only-correctness, not unmodifiable-correctness.
Sep 8, 2014 at 1:01am
I think this is a good demonstration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int i = 0;

void display_number(const int& x)
{
    using std::cout;
    using std::endl;

    cout<< "i = "<< x<< endl;
    i++;
    cout<< "i = "<< x<< endl;
}

int main(int c, char **v)
{
    display_number(i);
    return 0;
}
Last edited on Sep 8, 2014 at 1:06am
Sep 8, 2014 at 1:16am
The function display_number() promises not to change the state of the program via its parameter. It may change the state of the program in any number of other ways, however.
Pages: 12