TAILIEUCHUNG - Software Engineering For Students: A Programming Approach Part 28

Software Engineering For Students: A Programming Approach Part 28. This fully revised version of Doug Bell's Software Engineering: A Programming Approach continues to use the successful formula of the previous editions. The author's approach is to present the main principles, techniques and tools used in software engineering, one by one, chapter by chapter. This book is a unique introduction to software engineering for all students of computer science and its related disciplines. It is also ideal for practitioners wishing to remain current with new developments in the area | 248 Chapter 17 Software robustness Let us turn to examining how an exception is thrown using the same example. In Java the method parseint can be written as follows -----------------------------------------------------------------------------1 public int parseInt String string throws NumberFormatException int number 0 for int i 0 i i char c i if c 0 c 9 throw new NumberFormatException number number 10 c - 0 return number ------------------------------------------------------------------------------ You can see that in the heading of the method the exception that may be thrown is declared along with the specification of any parameters and return value. If this method detects that any of the characters within the string are illegal it executes a throw instruction. This immediately terminates the method and transfers control to a catch block designed to handle the exception. In our example the catch block is within the method that calls parseint. Alternatively the try-catch combination can be written within the same method as the throw statement. Or it can be written within any of the methods in the calling chain that led to calling parseint. Thus the designer can choose an appropriate place in the software structure at which to carry exception handling. The position in which the exception handler is written helps both to determine the action to be taken and what happens after it has dealt with the situation. SELF-TEST QUESTION The method parseint does not throw an exception if the string is of zero length. Amend it so that it throws the same exception in this situation. What happens after an exception has been handled In the above example the catch block ends with a return statement which exits from the current method actionPerformed and returns control to its caller. This is the appropriate action in this case - the program is able to recover and continue in a useful way. In general the options are either to recover from the .

TỪ KHÓA LIÊN QUAN
TÀI LIỆU LIÊN QUAN