Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
```
// This code snippet shows how an external function can use a pattern like
// python's `try..except..else` with `.call`, `throw`, and reentry
contract TryExceptElsePatternUser {
address _sender;
function tryExceptElsePatternExample()
external // must be external
{
if( msg.sender == address(this) ) {
// `try` body - runs at +1 depth, msg.sender is this, true sender in storage
} else {
_sender = msg.sender;
if( !this.call(msg.data) ) {
// `except` body, run if "try" body throws
} else {
// `else` body - run if there is no exception
}
// EDIT: `finally`? body? I think it is not correct but can't remember why
}
}
}
```
@Victory
Copy link

Victory commented Jul 23, 2016

This is interesting, have you thought about what it would take to add a finally clause?

@nmushegian
Copy link
Author

nmushegian commented Aug 10, 2016

I remember trying and discovering something tricky that makes labeling the edited line not technically finally, but I can't remember why not

@veox
Copy link

veox commented Aug 10, 2016

Perhaps because finally has to perform non-conditional clean-up and then forward the exception (if any). It is not clear how to perform such a feat.

Just guessing. CMIIR

@k06a
Copy link

k06a commented Dec 12, 2017

@nmushegian do you think this will work with latest EVM?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment