官方文档:

https://solidity.readthedocs.io/en/develop/control-structures.html#error-handling-assert-require-revert-and-exceptions

require和assert方法的区别:

https://ethereum.stackexchange.com/questions/15166/difference-between-require-and-assert-and-the-difference-between-revert-and-thro

程序示例:

pragma solidity ^0.4.;

contract Sharer {
function sendHalf(address addr) public payable returns (uint balance) {
require(msg.value % == ); // Only allow even numbers
uint balanceBeforeTransfer = this.balance;
addr.transfer(msg.value / );
// Since transfer throws an exception on failure and
// cannot call back here, there should be no way for us to
// still have half of the money.
assert(this.balance == balanceBeforeTransfer - msg.value / );
return this.balance;
}
}

require和assert适用范围:

An assert-style exception is generated in the following situations:

  1. If you access an array at a too large or negative index (i.e. x[i] where i >= x.length or i < 0).
  2. If you access a fixed-length bytesN at a too large or negative index.
  3. If you divide or modulo by zero (e.g. 5 / 0 or 23 % 0).
  4. If you shift by a negative amount.
  5. If you convert a value too big or negative into an enum type.
  6. If you call a zero-initialized variable of internal function type.
  7. If you call assert with an argument that evaluates to false.

require-style exception is generated in the following situations:

  1. Calling throw.
  2. Calling require with an argument that evaluates to false.
  3. If you call a function via a message call but it does not finish properly (i.e. it runs out of gas, has no matching function, or throws an exception itself), except when a low level operation callsenddelegatecall or callcode is used. The low level operations never throw exceptions but indicate failures by returning false.
  4. If you create a contract using the new keyword but the contract creation does not finish properly (see above for the definition of “not finish properly”).
  5. If you perform an external function call targeting a contract that contains no code.
  6. If your contract receives Ether via a public function without payable modifier (including the constructor and the fallback function).
  7. If your contract receives Ether via a public getter function.
  8. If a .transfer() fails.

Internally, Solidity performs a revert operation (instruction 0xfd) for a require-style exception and executes an invalid operation (instruction 0xfe) to throw an assert-style exception. In both cases, this causes the EVM to revert all changes made to the state. The reason for reverting is that there is no safe way to continue execution, because an expected effect did not occur. Because we want to retain the atomicity of transactions, the safest thing to do is to revert all changes and make the whole transaction (or at least call) without effect. Note that assert-style exceptions consume all gas available to the call, while require-style exceptions will not consume any gas starting from the Metropolis release.

总得来说,两者都会使EVM回滚交易,不会造成任何变更。require适用于做输入检查,assert用来检查运行时内部错误(比如数组下标越界和除零错误)。require在Solidity Metropolis版本后将不消耗gas,而assert会消耗完所有的gas(gaslimit)。

https://medium.com/taipei-ethereum-meetup/%E6%AF%94%E8%BC%83-require-assert-%E5%92%8C-revert-%E5%8F%8A%E5%85%B6%E9%81%8B%E4%BD%9C%E6%96%B9%E5%BC%8F-30c24d534ce4

solidity错误处理的更多相关文章

  1. solidity 错误

    solidity版本 0.7.5 Member "transfer" not found or not visible after argument-dependent looku ...

  2. 智能合约语言 Solidity 教程系列9 - 错误处理

    这是Solidity教程系列文章第9篇介绍Solidity 错误处理. Solidity系列完整的文章列表请查看分类-Solidity. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文 ...

  3. 航空概论(历年资料,引之百度文库,PS:未调格式,有点乱)

    航空航天尔雅 选择题1. 已经实现了<天方夜谭>中的飞毯设想.—— A——美国2. 地球到月球大约—— C 38 万公里3. 建立了航空史上第一条定期空中路线—— B——德国4. 对于孔明 ...

  4. 以太坊solidity编程常见错误(不定期更新)

    1.报错: Expected token Semicolon got 'eth_compileSolidity' funtion setFunder(uint _u,uint _amount){ 解决 ...

  5. 安装solidity遇见的问题——unused variable 'returned'

    在编译安装solidity的过程中遇见了一个很奇怪的问题 webthree-umbrella/libethereum/libethereum/Executive.cpp: In member func ...

  6. 智能合约语言 Solidity 教程系列4 - 数据存储位置分析

    写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么 这部分的内容官方英文文档讲的不是很透,因此我在参考Soli ...

  7. 智能合约语言 Solidity 教程系列5 - 数组介绍

    写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么 本文前半部分是参考Solidity官方文档(当前最新版本: ...

  8. Solidity 中文文档 —— 第一章:Introduction to Smart Contracts

    第一章:智能合约简介 粗略地翻译了 Ethereum 的智能合约开发语言的文档:Solidity.欢迎转载,注明出处. 有任何问题请联系我,本人微信:wx1076869692,更多详情见文末. 我是 ...

  9. 智能合约语言 Solidity 教程系列8 - Solidity API

    这是Solidity教程系列文章第8篇介绍Solidity API,它们主要表现为内置的特殊的变量及函数,存在于全局命名空间里. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应 ...

随机推荐

  1. HDU - 6197:array array array (简单LIS)

    One day, Kaitou Kiddo had stolen a priceless diamond ring. But detective Conan blocked Kiddo's path ...

  2. bzoj 1864 三色二叉树

    Written with StackEdit. Description Input 仅有一行,不超过\(5*10^5\)个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次 ...

  3. 【java规则引擎】drools6.5.0版本中kmodule.xml解析

    kmodule.xml文件存放在src/main/resources/META-INF/文件夹下. <?xml version="1.0" encoding="UT ...

  4. 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

    传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...

  5. nginx中在超全局变量$_SERVER中增加变量

    业务中可能会用到一些自定义的超全局变量,需要在nginx中生成的,比如,每次nginx请求的id,可以在nginx中配置 如: location ~ \.php$ { root           / ...

  6. 调用 SSPI 失败,请参见内部异常 解决方法

    2017-11-12 12:49:53:706] OnServerConnectionAvailable error : System.Security.Authentication.Authenti ...

  7. FastAdmin + uni-app

    FastAdmin + uni-app FastAdmin https://www.FastAdmin.net uni-app http://uniapp.dcloud.io/ 自己先挖个坑.

  8. git diff的使用

    有时候可能睡觉时候忘记关电脑了,然后不小心触碰到键盘上某个神秘的按钮了,然后自己也不知道就提交了 就可能很悲剧 那么有时候不知道自己是否改变了哪些文件的内容 这时候就需要用到git diff git ...

  9. mysql复制原理与机制一

    复制原理:复制需要二进制日志记录数据库上的改变 slave的IO线程复制把master上的Binary log读取到本地的relay log里SQL线程负责把relay log恢复到数据库数据里 sh ...

  10. android签名生成和发布

    首先,我们需要一个keystore,当然已经有了的话就不用这一步了:cmd下:进入到jdk的bin目录,这样的话,android.keystore文件就会生成在这个目录下,签名的时候我们需要这个文件C ...