Error Handling - Tonga Institute of Higher Education

Download Report

Transcript Error Handling - Tonga Institute of Higher Education

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.
Compile Errors, Runtime Errors
and Logical Errors

Compile Errors – Errors that are found during the
compilation of a program


Runtime Errors – Errors that cause an abnormal
termination of a program


Your program is unable to compile
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
Common Compile Errors

Java is Case Sensitive


Any class, method or variable name must be written exactly the
same.
Error Message:



Symbol – A class, method or variable name
A semicolon is required at the end of every statement

Error Message:


cannot resolve symbol
’;’ 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
Common Compile Errors

Parenthesis must surround the condition in an IF
Statement

Error Message:


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:


‘(’ expected
Invalid method declaration; return type required
A method that returns something must have a return
value specified.

Error Message:

Missing return statement
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>
<classname>
is already defined in
Common Runtime Errors

The Equality Operator (==) is different from the
Assignment Operator (=)


Not calling System.Exit(0);


The program will run forever
Not including a Break statement in a Switch statement


This is required to exit a graphical user interface
Not providing a way for Loops to end


The IF Statement will not function properly
Undesired code will be run
Dividing a number by 0

Error Message:

java.lang.ArithmeticException: / by zero
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
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:
Ignore the exception
2. Handle the exception
1.

Use Try/Catch/Finally structure
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
}
Try/Catch/Finally Statement - 1
Code you are trying
Demonstration
Try/Catch/Finally 1
Try/Catch/Finally Statement - 2
Catches specific
Code you are trying
exception
Catches general exceptions
Demonstration
Try/Catch/Finally 2