10
CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey Peters, 2008 Copyright SAIT

CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Embed Size (px)

DESCRIPTION

Exceptions java.lang.Exception class defines “mild” error conditions that your program encounters java.lang.Error class defines serious error conditions Topic CPRG 215 Module 4.1- Errors and Exceptions Copyright SAIT

Citation preview

Page 1: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

CPRG 215

Introduction to Object-Oriented Programming with Java

Module 4- Exception and Error Handling

Topic 4.1 Errors and Exceptions

Produced by Harvey Peters, 2008Copyright SAIT

Page 2: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Please read the following sections in your textbook

Core Java, Volume I–Fundamentals, Eighth EditionBy Cay S. Horstmann & Gary Cornell•Chapter 11 - Exceptions, Logging, Assertions, and Debugging

•Dealing with Errors

CPRG 215 Module 4.1- Errors and ExceptionsCopyright SAIT

Page 3: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Exceptions• java.lang.Exception class defines “mild” error

conditions that your program encounters• java.lang.Error class defines serious error

conditions

Topic 4.1.1

CPRG 215 Module 4.1- Errors and ExceptionsCopyright SAIT

Page 4: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Exceptions• Exception handling allows a program to “recover”

from exceptions– When exception is encountered, an Exception object of the

type that occurred is generated and can be intercepted– Rather than crashing the program, special handling can

allow the program to work around the problem

Topic 4.1.1

CPRG 215 Module 4.1- Errors and ExceptionsCopyright SAIT

Page 5: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Exceptions• Exceptions can occur when:

– you try to open a file that does not exist– network connection is disrupted– operands being manipulated are outside of

prescribed ranges– the class file you are loading is missing– Many more…

CPRG 215 Module 4.1- Errors and ExceptionsCopyright SAIT

Topic 4.1.1

Page 6: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Exceptions• Errors can occur when:

– The system runs out of memory– The memory stack overflows– The virtual machine has an error– A method does not exist– A class definition does not exist– Many more…

CPRG 215 Module 4.1- Errors and ExceptionsCopyright SAIT

Topic 4.1.1

Page 7: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Exceptions• Common Exception classes

– ArithmeticException– NullPointerException– NegativeArraySizeException– ArrayIndexOutOfBoundsException– StringIndexOutOfBoundsException– SecurityException

CPRG 215 Module 4.1- Errors and ExceptionsCopyright SAIT

Topic 4.1.1

Page 8: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Exceptions• When to use Exception handling

– Exception processing causes extra overhead, reducing program efficiency

– If an Exception is regular, it is best to fix the program to handle the situation without causing an exception• Example: overflowing an Array – use the Array length

instead of a fixed number for loops• Example: converting a number that was entered in a

text field by the user – validate that the value contains only digits before converting

– If the Exception is random or unpredictable, trap Exceptions and handle them• Example: a file does not exist, the network connection

is down, the database connection failed

CPRG 215 Module 4.1- Errors and ExceptionsCopyright SAIT

Topic 4.1.1

Page 9: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Exceptions• Example:

Check example: Exceptionexample.java

CPRG 215 Module 4.1- Errors and ExceptionsCopyright SAIT

Topic 4.1.1

public class HelloWorld {public static void main (String args[ ]) {

int i = 0;String greetings [ ] = { “Hi!”, ”Yo!”, “Hey!” };while (i < 4) {

System.out.println (greetings[i]);

i++;}

}}- this should cause an exception as I moves out of the array bounds

Page 10: CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey

Exceptions• Built-in methods often throw exceptions

– Called the “Declare” approach– Java API documentation for Integer class parseInt()

method• public static int parseInt(String s) throws

NumberFormatException– “throws” indicates that the method can produce this

kind of exception– By declaring that the method throws the exception, it

will be passed from the method to the calling point to be processed

– If it is not handled in the program it causes the program to exit and the JVM handles it• We will examine the “Handle” approach in the

next topicCPRG 215 Module 4.1- Errors and Exceptions

Copyright SAIT

Topic 4.1.1