JF 6 2 Practice
JF 6 2 Practice
Lesson Objectives:
• Describe the different kinds of errors that can occur and how they are handled in Java
• Describe what exceptions are used for in Java
• Determine what exceptions are thrown for any foundation class
• Write code to handle an exception thrown by the method of a foundation class
Vocabulary:
Identify the vocabulary word for each definition below.
A keyword in Java that signals the following block of code handles a specified
exception.
This stops the interpreter from running the rest of the code until it finds a catch.
An error that occurs while the program is running, also known as an exception.
A block of code that handles exceptions by dealing with the exception if it is thrown.
Errors that occur during run-time and can be corrected or handled by your code.
2. What is wrong with the following code? It should print "Hello World!" four times to the screen.
String str = “Hello World”;
for(int i = 0; i < 4; i++);
{
System.out.println(str);
str+= ”!”;
}
Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
3. Describe an exception that you have experienced in your program before. Explain how it could be handled with a try/catch
block of code.
a. A syntax error
b. A logic error
c. An exception
Copyright © 2022, Oracle and/or its affiliates. Oracle, Java, and MySQL are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. 2