1 / 14

Expert Guide to Error Handling in Software Development

Learn how to identify and fix compile, runtime, and logical errors in your programs. Understand common error messages and practical tips for effective error handling in Java. Explore Java error handling techniques such as Try/Catch/Finally structure.

tommyk
Download Presentation

Expert Guide to Error Handling in Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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 • <signature> is already defined in <classname>

  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: • Ignore the exception • 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 (<type of exception> <exception variable name>) { //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 Catches specific exception Code you are trying Catches general exceptions

  14. Demonstration Try/Catch/Finally 2

More Related