Refer to the Canvas Module for this assignment
Introduction to Error Handling
Types of Errors - syntax,
logic, exceptions (runtime errors)
Unchecked and Checked exceptions - you MUST handle....
What to do when exceptions are thrown - catch them
try{ this part of the program might throw an exception }
catch (exception e){
this code will execute if the code in the try block throws an exception.
this code will handle the exception }
finally {
Common exceptions - also note the exceptions heirachy - exceptions are objects and are subclasses of the Throwable class. Two useful methods are getMessage() and printStackTrace();
- public class Testtrycatch2{
- public static void main(String args[]){
- try{
- int data=50/0;
- }catch(ArithmeticException e){System.out.println(e);}
- System.out.println("rest of the code...");
- }
- }
Sample use of mutliple catch statements:
public class TestMultipleCatchBlock{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e){System.out.println("task1 is completed");}
catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");}
catch(Exception e){System.out.println("common task completed");}
System.out.println("rest of the code...");
}
}
Section 6 Lesson 2: Handling Errors Vocabulary:
- A keyword in Java that signals the following block of code handles a specified exception.
-
An exception that is optional to be handled.
-
An exception that MUST be handled.
-
Indicates that there is a problem with interpreting your program.
-
This stops the interpreter from running the rest of the code until it finds a catch.
-
An error that indicates an issue with coding format.
-
An error that occurs while the program is running, also known as an exception.
-
An error that occurs as a result of incorrect programmer logic.
-
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.
Read Section 6 Lesson 2: Handling Errors slides
Complete Section 6 Lesson 2: Handling Errors questions 1-5.
Complete the Error Handling program and demonstrate for teacher check.
Complete 6-2 Project - this is a continuation of Project 5-2. Be sure to read the instructions because you are not meant to start this project from scratch, you start with Project 5-2 and save it as ProductPart3 and ProductTesterPart3 and then make additions to your current code according to the instructions.
Section 7 Lesson 1: Classes, Objects and Methods Vocabulary:
- A template used for making Java objects.
- An optional keyword used to access the members and methods of a class.
-
An instance of a class.
-
The operator used to create an instance of a class.
-
A built-in function of the Java VM that frees memory as objects are no longer needed or referenced.
-
A method that changes the state of an object.
-
A method that returns information about an object back to the calling program.
-
A procedure (changes the state of an object) or function (returns information about an object) that is encapsulated as part of a class.
-
A verb used to describe the act of creating a class object using the keyword new.
-
The process of assigning a default value to a variable. An object reference that has not been instantiated.
- An object reference that has not been instantiated.
- An optional method that is called just before an object is removed by the garbage collector.
-
The name of a variable that is associated with an object.
-
A special method used to create an instance of a class.