Created
May 15, 2013 16:25
-
-
Save Swatinem/5585263 to your computer and use it in GitHub Desktop.
The Java way of doing control flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the java way of doing control flow | |
try { | |
init; | |
for (;;) { | |
try { | |
if (!condition) | |
throw new ConditionNotMetExpection(); | |
if (foo) | |
throw new ContinueExpection(); | |
if (bar) | |
throw new BreakExpection(); | |
update; | |
} catch (ContinueExpection e) {} | |
} | |
} catch (BreakExpection e) {} | |
catch (ConditionNotMetExpection e) {} | |
// same shit for while/do-while | |
// and function calls: | |
void Method() throws Throwable { | |
throw new ReturnValueExpection(someValue); | |
} | |
Object returnValue; | |
try { | |
Method(); | |
} catch (ReturnValueExpection e) { returnValue = e.getValue(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why is this the Java way, you're the one throwing the exceptions here? Java even has labels so you can break multiple loops without resorting to stuff like this. Python has no labels, and uses exceptions for generators. So I don't know why you picked Java as your target.