Skip to content

Instantly share code, notes, and snippets.

@nmushegian
Last active March 26, 2022 16:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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
}
}
}
```
@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