1

I'm making my way through a python tutorial and came to a section that serves as an introduction to errors (getting familiar with errors, not freaking out when things break, etc). After digging deeper into a few of the errors, I've seen the phrase "raise an error" and "raise an exception".

Is this a specific function? If so, what's the benefit in "raising an error"? When visualizing this, it seems like the error that is produced when executing the code is being elevated above other code at runtime... can someone help me piece this together?

Thanks in advance!

  • 1
    "raise" is a synonym of "throw". It's a throwback to a jump command from C that would break the code out of a method and into an error handler. – duffymo Dec 03 '18 at 19:05
  • Actually, `raise` is a keyword in Python that you can use to deliberately make your code "crash" in some way. Exceptions can halt execution, if you wish, or you can "raise" an exception and make your code deal with that situation in a specific way – roganjosh Dec 03 '18 at 19:08

3 Answers3

5

Raising an Error is a programmers way of saying "something went wrong", in a very specific way. For an analogy, when a flag in a football game is thrown, it means a penalty has been committed. After the play finishes, the referee will say exactly what the penalty is (holding, pass interference, etc) and who the guilty party is.

In programming, the penalty is equivalent to the error throwing, the penalty type is the error type, and the guilty party is based on what diagnostics you the programmer provide (widely varies between programming languages).

rchav9
  • 301
  • 4
  • 13
0

You can think of it as: You want to raise an alarm(exception) as soon as you see fire(error condition).

Refer:
https://docs.python.org/2.0/ref/raise.html

Also refer:
Manually raising (throwing) an exception in Python

ak_app
  • 170
  • 10
0

When programmers refer to raise an error it means to catch an unexpected behaviour what something goes wrong. As a simple example in Python:

int('a')
>>
----> 1 int('a')

ValueError: invalid literal for int() with base 10: 'a'

This makes sense, cause intuitively, you won't expect any program to cast a string value like a into a integer unless explicitly defined somewhere. And there are benefits to this, if the error was never raised, an unexpected behaviour might occur that breaks the program flow without anybody knowing.

A IRL example would be for example your car, if you are low on gas, you are notified through an indicator on your dashboard display, that is like a form of a notification raised to let you know you are low on gas that allows you to make an informed decision. Similarly, when you program, good errors are raised to inform you that something is not working accordingly and allows you to make an informed decision on how to manage or mitigate it.

As an example:

def multiply(a, b):
    try:
        return int(a) + int(b)
    except ValueError as e:
        print('{}, one or both inputs cannot be converted'.format(e))

multiply('1', 'a')
>> invalid literal for int() with base 10: 'a', one or both inputs cannot be converted

Because there was a ValueError raised, I am able to catch it and inform the user that one of the inputs has failed. If nothing was raised, it would be similar as driving your car not knowing when the gas is finished leading you to confusion.

How are these errors raised? If I have a function to check whether a value is in the range of 10 I can do it like:

def in_range_10(n):
    for i in range(1,11):
        if i == n:
            return i
    raise IndexError(n)

Every value from 1 to 10 will return the value itself because it is in range, but if the value goes above 10, an error is throw with raise IndexError and the value itself.

in_range_10(11)
>> ...
      3         if i == n:
      4             return i
----> 5     raise IndexError(n)

IndexError: 11
BernardL
  • 5,162
  • 7
  • 28
  • 47