Skip to content

Instantly share code, notes, and snippets.

@nmushegian
Last active March 26, 2022 16:04
Show Gist options
  • Save nmushegian/de35e6a389b12da4f9bc1a828b44c5a6 to your computer and use it in GitHub Desktop.
Save nmushegian/de35e6a389b12da4f9bc1a828b44c5a6 to your computer and use it in GitHub Desktop.
```
// 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

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