官方文档:

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. Eclipse环境搭建配置操作

    1.选择window 2.设置字体 3.设置编码格式:国际编码:UTF-8 第一个地方设置编码格式 第二个地方设置编码格式:这个更重要些 4.配置26个英文小写字母. 作用:能够在开发时提示你,快速开 ...

  2. IOS socket编程--Asyncsocket

    iPhone的标准推荐是CFNetwork 库编程,其封装好的开源库是 cocoa AsyncSocket库,用它来简化CFNetwork的调用,它提供了异步操作 主要特性有: 队列的非阻塞的读和写, ...

  3. JDBC 3 通过PreparedStatement 对数据库进行增删改查

    下面程序沿用上面的封装. 1 插入数据 public boolean ChaRu3(User user){ boolean flag=true; Connection conn=null; Prepa ...

  4. WPF 自定义BarChartControl(可左右滑动的柱状图)(转)

    自定义可左右滑动.拖拽滑动的平面柱状图 在做这种样式控件之前,可先浏览我之前预研的控件: A.自定义左右滑动ScrollViewer(可拖动滑动) B.自定义Bar柱状图 OK,现在说下控件具体设计过 ...

  5. gogs docker 安装

     1.  gogs 镜像      docker pull gogs/gogs    2.  mysql    docker mysql    3.  本地数据卷配置 mkdir gogs & ...

  6. 一键获取 所有连接过的WIFI密码

    使用方法 一.运行CMD (以及 开启无线网卡.最好是笔记本) 二.输入命令: for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh ...

  7. EasyWeChat微信开放平台第三方平台接入

    EasyWeChat微信开放平台第三方平台接入 https://www.cnblogs.com/bainiu/p/8022729.html

  8. mysql命令 show slave status\G;命令输出详解

    show slave status\G; 命令输出详解 mysql> show slave status\G; *************************** . row ******* ...

  9. VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法

    1.首先,要用#import语句来引用支持ADO的组件类型库(*.tlb),其中类型库可以作为可执行程序 (DLL.EXE等)的一部分被定位在其自身程序中的附属资源里,如:被定位在msado15.dl ...

  10. Maven入门----介绍及环境搭建(一)

    知识点: 介绍Maven 本机搭建Maven环境 DEMO测试 本地仓库迁出 Maven简介: 百度百科: 说到底就是一个项目管理工具. 本机搭建Maven环境: Maven的环境需要jdk环境的支持 ...