Exceptions in Ruby and its types
Hi, This is Shubham Mishra from India, this is the part of Ruby on Rails exploration journey. In this post we will discuss about the ruby's some special concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started...
Popular programming language Ruby is renowned for its ease of use and adaptability. Unfortunately, Ruby is susceptible to mistakes that can happen while executing code, just like any other programming language. These mistakes are known as exceptions, and Ruby includes a sophisticated method for dealing with them. We'll go through exceptions in this blog post, including what they are, how Ruby handles them, and the many kinds of exceptions you might run against.What are exceptions in Ruby?
A programming error is an exception. It takes place while the programme is running. When an exception occurs, Ruby leaps to a predetermined section of code known as a "rescue" block, interrupting the program's normal flow. The rescue block is intended to deal with the exception and offer a mechanism to correct the mistake. Objects that are instances of the Exception class or one of its subclasses are exceptions in the Ruby programming language. Each exception provides a backtrace that reveals where the error happened in the code as well as a message that details the error.How exceptions works in Ruby?
The "raise" keyword in Ruby can be used to raise an exception. Ruby looks for the closest rescue block that can handle the exception when an exception is raised by looking through the call stack. In the absence of a rescue block, the programme ends with an error. An illustration of how to raise an exception in Ruby is given here: In this example, the "divide" method raises an exception if the second argument is zero. When we call the "divide" method with 10 and 0 as arguments, an exception is raised with the message "Cannot divide by zero".Types of exceptions in Ruby
Ruby has many built-in exception classes that cover a wide range of errors that can occur during program execution. Here are some of the most common exception classes: 1. StandardError: This is the base class for most exceptions in Ruby. It includes errors such as ArgumentError, NameError, and RuntimeError. 2. ArgumentError: This exception is raised when a method is called with the wrong number of arguments. 3. NameError: This exception is raised when a variable or method is referenced that doesn't exist. 4. RuntimeError: This exception is raised when an error occurs that doesn't fit into any other exception class. 5. NoMethodError: This exception is raised when a method is called on an object that doesn't have that method defined. 6. TypeError: This exception is raised when a method is called with an argument of the wrong type. 7. ZeroDivisionError: This exception is raised when division by zero occurs. And many more, refer: Ruby all exceptions classes These are just a few examples of the many exception classes that Ruby provides. By using these exception classes and creating your own custom exception classes, you can write code that handles errors gracefully and provides a better user experience. Exceptions are a crucial component of every programming language, and Ruby provides a sophisticated system for handling them. You may write code that is more reliable and offers a better user experience by being aware of how Ruby handles exceptions and the many kinds of exceptions that can occur. You can explore my previous blog: Ruby on Rails secure code approach and practices Or you can explore my other blogs too here,Get to know answers for common search on Google : A blog for posts which can help you for daily life problems, such as where to get free images, Topic suggestion for the blog.
Computer Science algorithms and other knowledge share : A blog for posts such as best search algorithm, Top interview questions for diffrent technologies, knowledge share for some frameworks or programming languages for the interview or in general terms.
My ideas to solve real world problems : A blog where me shared and presented my ideas to solve a real world problems, this will be interesting for me.
Ruby on Rails Web development Blog : As the name suggest, it is the blog for sharing few knowledge about RoR web development framework.
Liked my blogs, wanna to connect:
Thanks for reading this post, Have a good day :)
Comments
Post a Comment