Learn Solidity - Build Decentralized Application in Ethereum

Learn Solidity: Exceptions in Solidity

In this post, we will learn how Exceptions work in Solidity Language. [pastacode lang=”javascript” manual=”pragma%20solidity%200.4.8%3B%0A%0A%20%20%20%20%2F*%0A%20%20%20%20*%20%40title%20A%20Simple%20Example%0A%20%20%20%20*%20%40author%20Toshendra%20Sharma%0A%20%20%20%20*%20%40notice%20Example%20for%20the%20Solidity%20Course%0A%20%20%20%20*%20%0A%20%20%20%20*%2F%0A%0A%2F%2F%20There%20are%20some%20cases%20where%20exceptions%20are%20thrown%20automatically.%20%0A%2F%2F%20Otherwise%20You%20can%20use%20the%20throw%20instruction%20to%20throw%20an%20exception%20manually.%20%0A%2F%2F%20The%20effect%20of%20an%20exception%20is%20that%20the%20currently%20executing%20call%20is%20%0A%2F%2F%20stopped%20and%20reverted.%20It%20means%20all%20changes%20to%20the%20state%20and%20balances%20are%20undone%20%0A%2F%2F%20and%20the%20exception%20is%20also%20%E2%80%9Cbubbled%20up%E2%80%9D%20through%20Solidity%20function%20calls%20%0A%2F%2F%20when%20exceptions%20are%20send%20all%20the%20low-level%20functions%20call%2C%20delegate%20calls%20and%20call%20code%2C%20%0A%2F%2F%20will%20return%20false.%0A%0A%2F%2F%20It%20is%20interesting%20to%20note%20that%20Catching%20exceptions%20is%20not%20yet%20possible.%0A%0A%2F%2F%20Lets%20see%20an%20example%2C%20to%20know%20how%20a%20throw%20can%20be%20used%20to%20easily%20revert%20%0A%2F%2F%20an%20Ether%20transfer%20and%20also%20how%20to%20check%20the%20return%20value%20of%20send.%20%0A%0Acontract%20Sharer%20%7B%0A%20%20%20%20function%20sendHalf(address%20addr)%20payable%20returns%20(uint%20balance)%20%7B%0A%20%20%20%20%20%20%20%20if%20(!addr.send(msg.value%20%2F%202))%20%2F%2F%20send%20half%20of%20the%20ether%20to%20the%20addr%20mentioned%20in%20the%20call.%0A%20%20%20%20%20%20%20%20%20%20%20%20throw%3B%20%2F%2F%20here%20throw%20is%20a%20User%20Generated%20Exception%20and%20it%20also%20reverts%20the%20transfer%20to%20Sharer%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20assert(condition)%20can%20also%20generate%20a%20User%20Exception%20%20%0A%20%20%20%20%20%20%20%20return%20this.balance%3B%0A%20%20%20%20%7D%0A%7D%0A%0A%20%20%0A%0A%2F%2F%20Currently%2C%20Solidity%20automatically%20generates%20a%20runtime%20exception%20in%20the%20following%20situations%3A%0A%0A%2F%2F%20If%20you%20access%20an%20array%20at%20a%20too%20large%20or%20negative%20index%20(i.e.%20x%5Bi%5D%20where%20i%20%3E%3D%20x.length%20or%20i%20%3C%200).%0A%0A%2F%2F%20If%20you%20access%20a%20fixed-length%20bytesN%20at%20a%20too%20large%20or%20negative%20index.%0A%0A%2F%2F%20If%20you%20call%20a%20function%20via%20a%20message%20call%20but%20it%20does%20not%20finish%20properly%20%0A%2F%2F%20(i.e.%20it%20runs%20out%20of%20gas%2C%20has%20no%20matching%20function%2C%20or%20throws%20an%20exception%20itself)%2C%20%0A%2F%2F%20except%20when%20a%20low%20level%20operation%20call%2C%20send%2C%20delegatecall%20or%20callcode%20is%20used.%20%0A%2F%2F%20The%20low%20level%20operations%20never%20throw%20exceptions%20but%20indicate%20failures%20by%20returning%20false.%0A%0A%2F%2F%20If%20you%20create%20a%20contract%20using%20the%20new%20keyword%20but%20the%20contract%20creation%20does%20not%20%0A%2F%2F%20finish%20properly%20(see%20above%20for%20the%20definition%20of%20%E2%80%9Cnot%20finish%20properly%E2%80%9D).%0A%0A%2F%2F%20If%20you%20divide%20or%20modulo%20by%20zero%20(e.g.%205%20%2F%200%20or%2023%20%25%200).%0A%0A%2F%2F%20If%20you%20shift%20by%20a%20negative%20amount.%0A%0A%2F%2F%20If%20you%20convert%20a%20value%20too%20big%20or%20negative%20into%20an%20enum%20type.%0A%0A%2F%2F%20If%20you%20perform%20an%20external%20function%20call%20targeting%20a%20contract%20that%20contains%20no%20code.%0A%0A%2F%2F%20If%20your%20contract%20receives%20Ether%20via%20a%20public%20function%20without%20payable%20modifier%20%0A%2F%2F%20(including%20the%20constructor%20and%20the%20fallback%20function).%0A%0A%2F%2F%20If%20your%20contract%20receives%20Ether%20via%20a%20public%20getter%20function.%0A%0A%2F%2F%20If%20you%20call%20a%20zero-initialized%20variable%20of%20internal%20function%20type.%0A%0A%2F%2F%20If%20a%20.transfer()%20fails.” message=”” highlight=”” provider=”manual”/]
Read More