> For the complete documentation index, see [llms.txt](https://solstep.gitbook.io/solidity-steps/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://solstep.gitbook.io/solidity-steps/step-5/43-try-and-catch.md).

# 43: Try and Catch

In Solidity try and catch can be used to handle exceptions. This can prevent a transaction from failing due to an invalid input or a bug in the code.

* **Example 1** In the following example, we want to prevent someone from sending more than 5 ether in a single transaction.

```solidity
function sendMoney(address _to, uint256 _amount) public payable {    
    try {        
        require(_amount <= 5 ether);        
        _to.transfer(_amount);    
    } 
    catch (bytes32 err) {        
        revert("Only 5 ether can be sent at a time");    
    }
}
```

In this example, if the \_amount is greater than 5 ether, the require statement will throw an exception and the catch block will handle the exception, reverting the transaction and sending a message to the sender.

* **Example 2** In this example, we want to ensure that the sender and receiver are not the same address.

```solidity
function sendMoney(address _to, uint256 _amount) public payable {    
    try {        
        require(_to != msg.sender);        
        _to.transfer(_amount);    
    } 
    catch (bytes32 err) {        
        revert("Sender and receiver cannot be the same address");    
    }
}
```

If the sender and receiver are the same address, the require statement will throw an exception and the catch block will handle the exception, reverting the transaction and sending a message to the sender.

*That's it for the lesson 43! In the next lesson, Library*


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://solstep.gitbook.io/solidity-steps/step-5/43-try-and-catch.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
