Presentation is loading. Please wait.

Presentation is loading. Please wait.

Error Handling.

Similar presentations


Presentation on theme: "Error Handling."— Presentation transcript:

1 Error Handling

2 COSC 494: Human-Centric Software Engineering
State of the practice in SE tools Demo tools in class State of the art in SE research Discuss research papers Adv topics in SE Code reviewing, continuous integration Program analysis, empirical methods Design, implement, and evaluate an SE tool Individual project

3 var contents = File.ReadAllText(”settings.txt");
What if file isn’t there? CRASH!

4 if(File. Exists(“settings. txt”)) { var contents = File
if(File.Exists(“settings.txt”)) { var contents = File.ReadAllText(”settings.txt"); } What if file isn’t there? CRASH!

5

6 Does your program handle everything?
File doesn’t exist File contents are not as expected Connection to database fails Users enter invalid input

7 Check for errors What assumptions are you making?
Could a value be null? Are there error codes? Special cases? String.IndexOf() Returns -1 if doesn’t exist StringReader.ReadLine() Returns null if there are no more Enumerable.Filter() Returns [] if doesn’t exist String.Contains() Returns false if doesn’t exist String.Substring() Returns “” if doesn’t exist

8 How to handle errors? End the program Ostrich algorithm
Notify the user with clear explanation Return the program to a valid state Retry Catch the exception

9 Retry Logic data = httpClient.Get(“ attempts = 0; do { attempts++; data.status = httpClient.Get(“ if(data == SUCCESS) { break; } sleep(delay); } while (tries < maxAttempts); HandleFailure();

10 Exception Handling try { // Code that might fail } catch { // Code to handle the failure finally { // Optional: clean up after the try and catch

11 Downsides of error handling
Lots of added code Makes code complex and hard to read Hard to test Exceptions are fancy GOTOs Advice? Reduce the number of potential errors Recover when possible Provide helpful error messages Keep code clean Don’t be lazy

12 A bad error message

13 A bad error message

14 A good error message

15 Gracefully handle crashes
when possible Provide helpful message to user Log the crash along with info to debug The error/exception Where it occurred Any info to help replicate Clean up Don’t leave files in corrupt state Close connections and deallocate memory


Download ppt "Error Handling."

Similar presentations


Ads by Google