14
Error Handling Tonga Institute of Higher Education

Error Handling Tonga Institute of Higher Education

Embed Size (px)

DESCRIPTION

Compile Errors, Runtime Errors and Logical Errors Compile Errors – Errors that are found during the compilation of a program  Your program is unable to compile Runtime Errors – Errors that cause an abnormal termination of a program  Your program crashes Logical Errors – Errors that are not found during the compilation of a program and do not cause an abnormal termination of the program  These are the most difficult to find and often result in very big problems.  You set an employees pay check to be $500,000 instead of $50,000

Citation preview

Page 1: Error Handling Tonga Institute of Higher Education

Error Handling

Tonga Institute of Higher Education

Page 2: Error Handling Tonga Institute of Higher Education

Fixing Errors Bug – An error in a program. Debug - To find and remove errors (bugs) from a

software program. Read the error! Use the task list to quickly take you to the source of your

error. Try to fix the error yourself before asking for help. If you ask for help, be able to describe what's going on. The more experience you have with errors, the better

you will be able to fix them in the future.

Page 3: Error Handling Tonga Institute of Higher Education

Compile Errors, Runtime Errors and Logical Errors Compile Errors – Errors that are found during the

compilation of a program Your program is unable to compile

Runtime Errors – Errors that cause an abnormal termination of a program Your program crashes

Logical Errors – Errors that are not found during the compilation of a program and do not cause an abnormal termination of the program These are the most difficult to find and often result in very big

problems. You set an employees pay check to be $500,000 instead of

$50,000

Page 4: Error Handling Tonga Institute of Higher Education

Common Compile Errors Java is Case Sensitive

Any class, method or variable name must be written exactly the same.

Error Message: cannot resolve symbol

Symbol – A class, method or variable name A semicolon is required at the end of every statement

Error Message: ’;’ expected

Brackets and Parenthesis must occur in matching pairs Every time we open a bracket or parenthesis, we need to close

it. Error Message:

‘}’ expected ‘)’ expected

Page 5: Error Handling Tonga Institute of Higher Education

Common Compile Errors Parenthesis must surround the condition in an IF

Statement Error Message:

‘(’ expected Must Initialize a Variable before using it

Error Message: Variable <variablename> might not have been initialized

Must specify a return type for each method Error Message:

Invalid method declaration; return type required A method that returns something must have a return

value specified. Error Message:

Missing return statement

Page 6: Error Handling Tonga Institute of Higher Education

Common Compile Errors A method that returns something must return the

correct type Error Message:

Incompatible types A method that doesn’t return anything must not

use the return keyword Error Message:

Cannot return a value from method whose result type is void

Creating an overloaded method with the same signature <signature> is already defined in <classname>

Page 7: Error Handling Tonga Institute of Higher Education

Common Runtime Errors The Equality Operator (==) is different from the

Assignment Operator (=) The IF Statement will not function properly

Not calling System.Exit(0); This is required to exit a graphical user interface

Not providing a way for Loops to end The program will run forever

Not including a Break statement in a Switch statement Undesired code will be run

Dividing a number by 0 Error Message:

java.lang.ArithmeticException: / by zero

Page 8: Error Handling Tonga Institute of Higher Education

Java Error Handling

Java can gracefully handle runtime errors Things you may want to do in your error

handling Display an error message Allow user to contact technical support Allow user to send error data to creator of software Save data Release any resources that are being used

Page 9: Error Handling Tonga Institute of Higher Education

Exceptions

Exception – An error that occurred that can be handled by Java

An exception is a class When an exception occurs, we can do 2

things:1. Ignore the exception2. Handle the exception

Use Try/Catch/Finally structure

Page 10: Error Handling Tonga Institute of Higher Education

Try/Catch/Finally Structure Use the Try/Catch/Finally structure to handle exceptions

You can catch all exceptions or a particular type of an exception The finally structure is optional

try {//Code that may cause an exception

} catch (<type of exception> <exception variable name>) {//Code that handles the exception

} finally {//Code to always run after the try or catch code is run

}

Page 11: Error Handling Tonga Institute of Higher Education

Try/Catch/Finally Statement - 1Code you are trying

Page 12: Error Handling Tonga Institute of Higher Education

Demonstration

Try/Catch/Finally 1

Page 13: Error Handling Tonga Institute of Higher Education

Try/Catch/Finally Statement - 2Code you are trying

Catches specificexception

Catches general exceptions

Page 14: Error Handling Tonga Institute of Higher Education

Demonstration

Try/Catch/Finally 2