Chapter 1 - Errors - Everett School District
Download
Report
Transcript Chapter 1 - Errors - Everett School District
BUILDING JAVA
PROGRAMS
CHAPTER 1
ERRORS
OBJECTIVES
Recognize different errors that Java uses and how to fix
them.
2
LET’S GET STARTED
When you write programs, there are three types of errors
that you may introduce… what are they?
•
•
•
Syntax Errors
Logic Errors
Runtime Errors
3
SYNTAX ERROR EXAMPLE
Where are the errors in the following program?
1
2
3
4
5
public class Hello {
pooblic static void main(String[] args) {
System.owt.println("Hello, world!")
}
}
Compiler output:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
Hello.java:3: ';' expected
}
^
2 errors
•
•
The compiler shows the line number where it found the error.
The error messages can be tough to understand!
4
LOGIC ERROR EXAMPLE
Where are the errors in the following program?
1
2
3
4
5
6
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, “
+ “world!");
}
}
Compiler output?
• Nothing! Only the programmer can catch logic errors.
Program output?
• Unexpected: Hello, World!
5
RUNTIME ERROR EXAMPLE
Where are the errors in the following program?
1
2
3
4
5
public class Hello {
public static void main(String[] args) {
Function1DividedBy0();
}
}
Compiler output?
• Nothing. (Unless you have a smart compiler!)
Program output?
• Crash!
6
WITH A PARTNER (OR TWO)
• Divide your notebook into three sections: Syntax,
Logic, and Runtime. For each section, see how
many different types of errors you can come up
with.
• You can use JGrasp to help you think of errors.
• Hint: Start from a working version of the Hello.java
program.
• Here are some examples to get you started
• Syntax: Missing a semicolon
• Logic: Calling the wrong function
• Runtime: Dividing by 0
7