Exceptions and pre-conditions: Difference between revisions
Mr. MacKenty (talk | contribs) (Created page with "right|frame|Exceptions<ref>http://www.flaticon.com/</ref> An exception is an anomalous or exceptional condition requiring special processing – often cha...") |
Mr. MacKenty (talk | contribs) No edit summary |
||
Line 7: | Line 7: | ||
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 write conditional statements if an exception occurs, | We use the term "catch an exception" to write conditional statements if an exception occurs, how should the program respond. | ||
== Identifying 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 | |||
== Example == | == Example == | ||
I am writing a program to calculate your final grade. The program accepts as input all your grades, averages them, and then outputs a final score. | I am writing a program to calculate your final grade. The program accepts as input your name, all your grades, averages them, and then outputs your name and a final score. | ||
What exceptions might we consider? | |||
# What if a student is missing a score? | |||
# What if a score is non-standard (instead of an integer, we have a letter)? | |||
# What if a there is non-standard character in a student name? | |||
== Do you understand this? == | == Do you understand this? == |
Revision as of 15:21, 24 July 2017
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 write conditional statements if an exception occurs, how should the program respond.
Identifying exceptions[edit]
- 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
Example[edit]
I am writing a program to calculate your final grade. The program accepts as input your name, all your grades, averages them, and then outputs your name and a final score.
What exceptions might we consider?
- What if a student is missing a score?
- What if a score is non-standard (instead of an integer, we have a letter)?
- What if a there is non-standard character in a student name?
Do you understand this?[edit]
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]
- ↑ http://www.flaticon.com/
- ↑ https://en.wikipedia.org/wiki/Exception_handling
- ↑ https://stackoverflow.com/questions/5813614/what-is-difference-between-errors-and-exceptions
- ↑ IB Diploma Programme Computer science guide (first examinations 2014). Cardiff, Wales, United Kingdom: International Baccalaureate Organization. January 2012.