Exceptions and pre-conditions: Difference between revisions

From Computer Science Wiki
 
(11 intermediate revisions by the same user not shown)
Line 5: Line 5:
Be careful not to confuse an exception with an error.  
Be careful not to confuse an exception with an error.  


An error "indicates serious problems that a reasonable application should not try to catch." An Exception "indicates conditions that a reasonable application might want to catch."<ref>https://stackoverflow.com/questions/5813614/what-is-difference-between-errors-and-exceptions</ref>.
An error "indicates serious problems that a reasonable application should not try to catch." An exception "indicates conditions that a reasonable application might want to catch."<ref>https://stackoverflow.com/questions/5813614/what-is-difference-between-errors-and-exceptions</ref>.


We use the term "catch an exception" to catch exceptions and plan how the program should respond.  
We use the term "catch an exception" to catch exceptions and plan how the program should respond.  


== Identifying exceptions ==
The exceptions below are common. There are many other types of exceptions.


# consider different data types (are you passing an [[int]] to a function which is expecting a [[string]]?)
# consider inaccurate user input (accidental or deliberate)
# consider writing or reading a to file - there could be a failure opening, writing, or closing to file
# consider memory limitations - this is especially important when you are working with large datasets
# consider the possibility a user might attempt to interrupt the program (usually by pressing control-C).
For a list of common exceptions, please [https://www.tutorialspoint.com/python/standard_exceptions.htm click here]


== Example ==
== Example ==
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
# here is some code with a deliberate error. There should be two apostrophe's for the end argument.
# here is some Python code with a deliberate error. There should be two apostrophe's for the end argument.
print("hello world", end=')
print("hello world", end=')
print("from Warsaw")
print("from Warsaw")
print("=" * 25)
print("=" * 25)
</syntaxhighlight>
</syntaxhighlight>
When we execute this code we see the following exception:




[[File:Exception example.png]]
[[File:Exception example.png|700px]]
 
You can see there are four lines here:
 
# on the first line we see the name of the file being executed and the '''line number''' where the exception was thrown.
# on the second line we see the line of code where the exception was thrown
# on the third line we see a carat (^) pointing the place where the exception was thrown
# on the fourth line we see the description of the exception
 
If you google the exception (line 4) you can often find helpful information about the exception.
For a list of common exceptions, please [https://www.tutorialspoint.com/python/standard_exceptions.htm click here]


== Standards ==  
== Standards ==  

Latest revision as of 08:09, 27 September 2021

Exceptions[1]

An exception is an anomalous or exceptional condition requiring special processing – often changing the normal flow of program execution[2].

Be careful not to confuse an exception with an error.

An error "indicates serious problems that a reasonable application should not try to catch." An exception "indicates conditions that a reasonable application might want to catch."[3].

We use the term "catch an exception" to catch exceptions and plan how the program should respond.


Example[edit]

# here is some Python code with a deliberate error. There should be two apostrophe's for the end argument.
print("hello world", end=')
print("from Warsaw")
print("=" * 25)

When we execute this code we see the following exception:


Exception example.png

You can see there are four lines here:

  1. on the first line we see the name of the file being executed and the line number where the exception was thrown.
  2. on the second line we see the line of code where the exception was thrown
  3. on the third line we see a carat (^) pointing the place where the exception was thrown
  4. on the fourth line we see the description of the exception

If you google the exception (line 4) you can often find helpful information about the exception. For a list of common exceptions, please click here

Standards[edit]

These standards are used from the IB Computer Science Subject Guide[4]

  • Identify exceptions that need to be considered in a specified problem solution.

References[edit]

  1. http://www.flaticon.com/
  2. https://en.wikipedia.org/wiki/Exception_handling
  3. https://stackoverflow.com/questions/5813614/what-is-difference-between-errors-and-exceptions
  4. IB Diploma Programme Computer science guide (first examinations 2014). Cardiff, Wales, United Kingdom: International Baccalaureate Organization. January 2012.