Introduction to Ruby raise
Many time when we are working with a certain difficult set of a scenario where it is very much possible that code will fail its execution (like opening a file), for example in case if we are going to write some content on any file and that file does not exist in this kind of cases code execution is going to stop which is not correct, we need to handle it. In Ruby we have a way to deal with these cases, that is raise statement. raise is a keyword in Ruby which allows us to raise any exception if it found, raise will throw an exception and that exception will be caught inside the rescue statement.
Below is the simple syntax for the raise statement in Ruby. In the below syntax there are two most important activities going on; they are given below.
- First, we are raising an exception.
- In the second activity, we rescue by using the keyword rescue.
begin raise # Inside the raise block the exception will be raise rescue # The raise exception will be captured here to handle. end
How does raise Statement Works in Ruby?
Before going deeper to explain about working we should be aware where we should use the raise statement, we can use the raise along with the rescue in case opening any file and in case handling database related queries. Because in these cases we cannot predict the nature of situations. The file can be there and cannot be there, in the same way it is possible that the query which we are writing for any tables may not be there inside the database and this can stop further execution of the code.
- Here every code which we write inside the begin block is protected, which means all the codes from the beginning to the rescue are in safe mode(any exception will be handled there). And in case we are trying to open any file or connecting to the databases and any exception happens, in that case, the raise will raise an issue.
- In case exceptions happen in the begin block it will halt and control will transfer between rescue and end. We can return a valid message for the user in case of exceptions from the raise block, and from this message, one can easily understand the type of exception.
- Every time for a rescue statement Ruby checks and compares the exception raised for each parameter.
Examples to Implement raise in Ruby
Below are the examples of raise in Ruby:
In the below example we have used a very simple approach to understand the flow of the raise in the Ruby. We have used the begin statement. We have done the raising and rescue part inside the begin statement. In the below example there are two most important activities going on; they are given below.
- First, we are raising an exception, here we can raise it in case it finds any situation where code stops execution if it continues the execution. So simply it will raise an issue and stop further execution.
- In the second activity, we rescue by using the keyword rescue. It will catch if it will find any raise statement.
Please see the below example.
begin # Here we are raising an issue raise 'Here we have raised an issue' puts 'I am after the raising an issue' # rescue for the issue rescue puts 'Capturing the raised issue here' end
In the below example we have used a simple flow of the raise statement. Let me explain the below example along with some steps. It is a functional approach to use the raise statement in Ruby.
- First, we have created a method raise_example method.
- We are calling the method raise_example at the end of the method.
- Inside the method raise_example, we are doing two things, one we are raising one exception and the second thing we are getting that raise issue inside the rescue statement.
- Both the work which is raise and rescue we have written inside the begin block which is a very safe side in Ruby to handle raise statements.
Please see the below example.
def raise_example begin puts 'This is going to get called before the raise statement!' # using raise to create an exception which will get capture in rescue raise 'Here we are creating an exception with raising it' puts 'This is after raise statement' # Using Rescue method rescue puts 'Now we are inside the rescue statement , it will rescue the raise issue' end puts 'This is the end of begin' end # calling method raise_example raise_example
Here we are using ensure along with the raise statement, ensue will confirm if the issue was raised or not. We can explain the raise statement in the below steps.
- It works almost similar to the simple try-catch, the only thing it has few more features like else, ensure to deal with exceptions. Here we have used the ensure statement along with the raise statement.
- We can see the example below, here we have rescued an exception which was raised inside the begin block and the rescue statement will fetch the data message, this can be any custom message also, and we will get the message raised by raise statement inside the rescue.
- Finally, we have also used the ensure statement which will ensure the issue.
Please follow the below example.
begin puts 'Searching for the file abc.txt,print success if the file is available' raise 'Searching for the file klm.txt,print the fail if file is not there' rescue Exception => exception puts exception.message puts exception.backtrace.inspect ensure puts "We are able to ensure that it was raise issue we need to take care of it" end
From this tutorial, we learned about the flow of raise statements in Ruby and we saw a very basic syntax of the Ruby, we learned the places where we can use the raise statement and also we learned the working flow of raise statements.
We hope that this EDUCBA information on “Ruby raise” was beneficial to you. You can view EDUCBA’s recommended articles for more information.