Presentation is loading. Please wait.

Presentation is loading. Please wait.

Error Analysis Runtime Errors.

Similar presentations


Presentation on theme: "Error Analysis Runtime Errors."— Presentation transcript:

1 Error Analysis Runtime Errors

2 Program States When we are developing a program the program is always in one of two states: Design Time Run Time Design Time: Is the state in which we are designing and developing the program. It is not executing in any way. Run Time: Is the state in which the code is actively being executed.

3 Runtime Errors A runtime error occurs while the program is running, usually when the program attempts an impossible operation. The result is the program crashing, this is the most jarring type of error since the user is the one that experiences it, not the programmer. The following slides will discuss common runtime errors and how to avoid them.

4 Runtime Error Examples
Remember that runtime errors occur when the program attempts an impossible operation: Dividing by Zero: common when the denominator is a variable Out of Bounds on an array: using an index < 0 or >= length Out of Bound on a String: accessing a single character, similar to an array but using an index outside of the possible index values Store incompatible type in a variable or array: such as a String in an int variable or array Null pointer: accessing a String array element or object that has not yet been initialized to a value Convert an invalid String to number type: E.g. “7.5” or “bob” to an int

5 Let’s Try… ERROR //Assume the user enters “37.5” hours int payPerHour;
int totalPay; int hours; totalPay = 685; System.out.print(“Weekly Hours Worked: “); hours = Integer.parseInt(input.nextLine()); payPerHour = totalPay / hours; System.out.println("Your pay is $" + payPerHour + " per hour"); Cannot convert “37.5” to an int, incompatible.

6 Remedy The best solution to combatting runtime errors is testing
Test your program in all ways that it can possibly be used in hopes of catching any problems. Unfortunately, this is a nearly impossible task on large scale programs. This is why when many video games are released people manage to crash their game so quickly Even a team of 200 testers on a game working for months will not be able to come up with all the scenarios 2 million average consumers will in a few hours. But we do the best we can…

7 The Debugger When you are building your program in Eclipse you should always run your code by either using the F11 key or the Bug symbol meant to run the program in Debug mode. When a runtime error occurs a popup message will appear asking you if you would like to switch to the Debug perspective, click Yes This changes the layout of Eclipse to help you deal with the problem. When you are done you can switch back to the standard Java Perspective by clicking the Java button at the top right of Eclipse.

8 The Debugger In the Debug Perspective, there are two windows that you should be concerned with: The code editor window in the middle of the screen The Debug window at the top The Debug window will show us all the subprogram calls that lead to the error We need to find one that is in one of our files, not a prebuilt Java library. In the example, we can see the bottom one points to line 14 of the Main class in our program. Double clicking this will take us to the error line so we can analyse the problem


Download ppt "Error Analysis Runtime Errors."

Similar presentations


Ads by Google