Presentation is loading. Please wait.

Presentation is loading. Please wait.

Error Handling Tonga Institute of Higher Education.

Similar presentations


Presentation on theme: "Error Handling Tonga Institute of Higher Education."— Presentation transcript:

1 Error Handling Tonga Institute of Higher Education

2 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.

3 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

4 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

5 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 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

6 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  is already defined in

7 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

8 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

9 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 exception 2. Handle the exception Use Try/Catch/Finally structure

10 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 ( ) { //Code that handles the exception } finally { //Code to always run after the try or catch code is run }

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

12 Demonstration Try/Catch/Finally 1

13 Try/Catch/Finally Statement - 2 Code you are trying Catches specific exception Catches general exceptions

14 Demonstration Try/Catch/Finally 2


Download ppt "Error Handling Tonga Institute of Higher Education."

Similar presentations


Ads by Google