COMP 202 Lecture 8: COMP 202 - Lecture 8

34 views3 pages
COMP 202: Foundations Of Programming
Lecture 8: Exceptions
Errors:
1. Stylistic Errors
Errors that do not affect the functionality of your code just makes it harder for someone else to read
and follow along.
E.g. bad indentation, bad variable names, no comments
2. Compile Time Errors
These errors are caught by the compiler. Generally, they are syntax problems (a missing semi-colon,
extra curly bracket, misspelled variable name etc.)
3. Logical Errors
These errors will not cause the program to crash. However, when you run your code it doesn’t work the
way you want it to
4. Run Time Errors
These errors (called exceptions) happen at run time and often depend on input/the values of the
variables. These errors cause your program to crash.
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the
program’s instructions.
Exceptions happen when the computer is asked to do something that doesn’t make sense:
- Integer division by zero
- Reading the wrong data type from Scanner
- Accessing an index of an array that doesn’t exist
- Accessing a null object
Jargon:
We say that a program “throws” an exception. We can alter the default behavior so that the program “catches”
the exception, and runs some other code.
Default Exception Handling:
By default, the program will terminate and print an angry message. This includes a call stack trace that indicates
where the exception occurred, and also shows the series of method calls that led to the current exception.
Throwing Exceptions:
If you want to communicate to the user of a method that they passed invalid input, or that some other kind of
error occurred, you can deliberately throw and Exception (causing the code to crash)
To throw an Exception in your code:
throw new [nameOfException](message)
Note that the variable type of the message is String, and is optional.
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in
jc123 and 40170 others unlocked
COMP 202 Full Course Notes
100
COMP 202 Full Course Notes
Verified Note
100 documents

Document Summary

Errors that do not affect the functionality of your code just makes it harder for someone else to read and follow along. E. g. bad indentation, bad variable names, no comments: compile time errors. Generally, they are syntax problems (a missing semi-colon, extra curly bracket, misspelled variable name etc. : logical errors. These errors will not cause the program to crash. However, when you run your code it doesn"t work the way you want it to: run time errors. These errors (called exceptions) happen at run time and often depend on input/the values of the variables. An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program"s instructions. Exceptions happen when the computer is asked to do something that doesn"t make sense: Reading the wrong data type from scanner. Accessing an index of an array that doesn"t exist. We say that a program throws an exception.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents