tailieunhanh - Lecture An introduction to Object-Oriented Programming with Java - Chapter 8: Exceptions and assertions
After you have read and studied this chapter, you should be able to: Improve the reliability of code by incorporating exception-handling and assertion routines, write methods that propagate exceptions, implement catch-try blocks for catching and handling thrown exceptions,. | Chapter 8 Exceptions and Assertions Chapter 8 Objectives After you have read and studied this chapter, you should be able to Improve the reliability of code by incorporating exception-handling and assertion routines. Write methods that propagate exceptions. Implement catch-try blocks for catching and handling thrown exceptions. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 Objectives After you have read and studied this chapter, you should be able to Write programmer-defined exception classes. Distinguish between checked and unchecked, or runtime, exceptions. Use assertions in methods to increase the chance of detecting bugs during development. Construct a program using the supervisor-subordinate design pattern. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Catching Exceptions An exception represents an error condition that can occur during the normal course of program execution. When an exception occurs, or is thrown, the normal sequence of flow is terminated. The exception-handling routine is then executed; we say the thrown exception is caught. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Catching Exceptions We can increase our programs’ reliability and robustness if we catch the exceptions ourselves using error recovery routines we develop. One way to do this is to wrap the statements that may throw an exception with the try-catch control statement. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Catching Exceptions inputStr = (null, prompt); try { age = (inputStr); } catch (NumberFormatException e){ (null, “’” + inputStr + ‘ is invalid\n” + “Please enter digits only”); ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Catching Exceptions Statements in the try block are executed in sequence. When . | Chapter 8 Exceptions and Assertions Chapter 8 Objectives After you have read and studied this chapter, you should be able to Improve the reliability of code by incorporating exception-handling and assertion routines. Write methods that propagate exceptions. Implement catch-try blocks for catching and handling thrown exceptions. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 Objectives After you have read and studied this chapter, you should be able to Write programmer-defined exception classes. Distinguish between checked and unchecked, or runtime, exceptions. Use assertions in methods to increase the chance of detecting bugs during development. Construct a program using the supervisor-subordinate design pattern. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Catching Exceptions An exception represents an error condition that can occur during the normal course of program execution. When an exception .
đang nạp các trang xem trước