
Software Faults, Errors, and Failures in Software Construction
Learn about the distinctions between faults, errors, and failures in software development. Explore how errors made during software construction can lead to defects and failures, and the different types of software problems that can arise, from compile-time failures to general failures that impact program output.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
CompSci 230 Software Construction Lecture Slides #22: Software faults & exceptions S1 2016
Agenda Topics: Preliminaries Faults, errors, failures Exceptions in Java Handling exceptions Exceptions as classes Custom exceptions 2 CompSci 230: FE
Software problems All software problems occur as a result of errors made during the construction of the software Errors are what humans make Not to be confused with error messages (manifestations of failures) Errors result in defects in the software Faults are defects that can cause the software to fail Not every defect is necessarily a fault (e.g., a wrong font for a label is a defect but it won t normally cause a program to fail) Faults are also known as bugs Failures are the results of faults being executed Terminology varies a bit here (software engineers like to dwell on this!) 3 CompSci 230: FE
Software problems compile-time failures Compile-time failures More or less guaranteed to be picked up by IDE or compiler automatically no need to test for these Need to fix them before we get anywhere! IDEs such as Eclipse offer features that help us avoid compile-time failures Code completion (helps avoid typos) Syntax analysis Code analysis (e.g., does the return value match the method declaration) Strictly typed languages turn potential runtime failures and failures in functionality into compile-time failures This reduces the amount of effort required for testing 4 CompSci 230: FE
Software problems runtime failures Runtime failures Program compiles but crashes when it runs In Java, this basically comes almost always in the form of an unhandled exception. Exception type tells us the kind of error (but not what led to it occurring) (Call) stack traces tell us where in the code the problem occurred (but not necessarily why) Typically happens for certain input only In other languages, crashes may manifest themselves in fatal error messages, segmentation faults, etc. Black box testing can try to provoke runtime failures Program stalls Typically: infinite loop 5 CompSci 230: FE
Software problems general failures Program compiles and does not crash, but doesn t produce correct output or behaviours Ideally caught during testing Need to have a good idea as to what expected output is E.g., consider navigation app finding a route that isn t obvious and appears incorrect but actually gets us to the destination faster How do we recognise that output is incorrect? 6 CompSci 230: FE
Software problems general failures "We are the ones who will hear," said Phouchg, "the answer to the great question of Life....!" "The Universe...!" said Loonquawl. "And Everything...!" "Shhh," said Loonquawl with a slight gesture. "I think Deep Thought is preparing to speak!" There was a moment's expectant pause while panels slowly came to life on the front of the console. Lights flashed on and off experimentally and settled down into a businesslike pattern. [ ]. "Good Morning," said Deep Thought at last. "Er..good morning, O Deep Thought" said Loonquawl nervously, "do you have...er, that is... - "An Answer for you?" interrupted Deep Thought majestically. "Yes, I have." The two men shivered with expectancy. Their waiting had not been in vain. "There really is one?" breathed Phouchg. "There really is one," confirmed Deep Thought. "To Everything? To the great Question of Life, the Universe and everything? - "Yes." [ ] "And you're ready to give it to us?" urged Loonquawl. "I am. - "Now? - "Now," said Deep Thought. They both licked their dry lips. "Though I don't think," added Deep Thought. "that you're going to like it." "Doesn't matter!" said Phouchg. "We must know it! Now! "Now?" inquired Deep Thought. "Yes! Now... "All right," said the computer, and settled into silence again. The two men fidgeted. The tension was unbearable. "You're really not going to like it," observed Deep Thought. "Tell us! "All right," said Deep Thought. "The Answer to the Great Question... - "Yes..! "Of Life, the Universe and Everything..." said Deep Thought. "Yes...!" "Is..." said Deep Thought, and paused. "Yes...!" "Is... -"Yes...!!!...?" "Forty-two," said Deep Thought, with infinite majesty and calm. Douglas Adams The Hitchhiker s Guide to the Galaxy 7 CompSci 230: FE
Software problems performance issues Program is too slow, or consumes too much memory Could be a fault (e.g., memory leak) Could be a wrong or in inefficient algorithm Could be a resource bottleneck (network, disk I/O, memory, configuration) Program doesn t crash but doesn t meet performance target Detection may require load testing Diagnosis may require logging or algorithm analysis E.g., analyse which methods are called most frequently / take longest to execute 8 CompSci 230: FE
Interlude: Exceptions in Java You already know exceptions: They are the way Java and many other programming languages tells us that something isn t right. E.g., you may have seen ArrayIndexOutOfBoundsException, NullPointerException, or ArithmeticException in Java Exceptions are thrown at the point where the code execution encounters the respective problem Unhandled exceptions crash a Java program, but we can handle exceptions by enclosing the code in which they are thrown in a try-catch block. This allows us to anticipate exceptions and provide an orderly way out of problematic situations 9 CompSci 230: FE
Handling Exceptions in Java public class ExceptionDemo { public static void main(String[] args) { // Try a divide: try { System.out.println("12 divided by 3 is " + Divider.divide(12, 3)); } catch (Exception e) { System.out.println("Can't divide 12 by 3: " + e.getMessage() ); } // Try another divide: try { System.out.println("11 divided by 0 is " + Divider.divide(11, 0)); } catch (Exception e) { System.out.println("Can't divide 11 by 0: " + e.getMessage() ); } } } 10 CompSci 230: FE
Built-in exceptions in Java All exceptions in Java are objects instantiated from Exception classes Built-in exception classes inherit from Exception Can be more precise on exception class to catch if we know what to expect: E.g., the following will catch an ArithmeticException but will not catch any ArrayIndexOutOfBoundsException that may occur here: try { System.out.println("Sorry, can't divide by 0"); } } System.out.println("11 divided by 0 is " + Divider.divide(a[3], b[26])); } catch (ArithmeticException e) { 11 CompSci 230: FE
Custom Exceptions In Java (and other languages), exceptions are objects based on classes Can subclass Exception and exception classes that derive from it This allows us to define exceptions with their own properties and behaviours E.g., we can store information in the exception object about the data that led to the exception being thrown We can throw such exceptions by creating a new exception object of the respective class public class CoffeeException extends Exception { public double temperature; public CoffeeException(double temperature) { super("Something is wrong with the coffee!"); this.temperature = temperature; } } 12 CompSci 230: FE
Throwing custom exceptions // Complain about the coffee: double temperature = 25; try { throw new CoffeeException(temperature); } catch (CoffeeException c) { System.out.println(c.getMessage()); if (c.temperature < 70) { System.out.println("It is too cold!"); } else if (c.temperature > 90) { System.out.println("It is too hot!"); } else { System.out.println("It just doesn't taste right!"); } } 13 CompSci 230: FE
Exceptions in Java methods If we will potentially throw an exception in a method that we will not catch in the method, we need to tell the compiler that the method may throw these exceptions: public void makeCoffee(int cups) throws CoffeeException, HotWaterException { // do something here throw new CoffeeException(45); } This allows the exceptions to be handled by the calling code, i.e., outside the method try { makeCoffee(2); } catch (CoffeeException c) { System.out.println("Bah!"); } 14 CompSci 230: FE
Review Do faults necessarily result in failures? Could a syntax error in Java class cause the class to throw an exception? 15 CompSci 230: FE