tailieunhanh - Lecture Object Oriented Programing - Chapter 13: Exception handling

In this chapter you will learn: How exception and error handling works; to use try, throw and catch to detect, indicate and handle exceptions, respectively; to use the finally block to release resources; how stack unwinding enables exceptions not caught in one scope to be caught in another scope; how stack traces help in debugging; how exceptions are arranged in an exception class hierarchy. | 13 Exception Handling It is common sense to take a method and try it. If it fails, admit it frankly and try another. But above all, try something. Franklin Delano Roosevelt O! throw away the worser part of it, And live the purer with the other half. William Shakespeare If they’re running and they don’t look where they’re going I have to come out from somewhere and catch them. Jerome David Salinger O infinite virtue! com’st thou smiling from the world’s great snare uncaught? William Shakespeare OBJECTIVES In this chapter you will learn: How exception and error handling works. To use try, throw and catch to detect, indicate and handle exceptions, respectively. To use the finally block to release resources. How stack unwinding enables exceptions not caught in one scope to be caught in another scope. How stack traces help in debugging. How exceptions are arranged in an exception class hierarchy. To declare new exception classes. To create chained exceptions that maintain complete | 13 Exception Handling It is common sense to take a method and try it. If it fails, admit it frankly and try another. But above all, try something. Franklin Delano Roosevelt O! throw away the worser part of it, And live the purer with the other half. William Shakespeare If they’re running and they don’t look where they’re going I have to come out from somewhere and catch them. Jerome David Salinger O infinite virtue! com’st thou smiling from the world’s great snare uncaught? William Shakespeare OBJECTIVES In this chapter you will learn: How exception and error handling works. To use try, throw and catch to detect, indicate and handle exceptions, respectively. To use the finally block to release resources. How stack unwinding enables exceptions not caught in one scope to be caught in another scope. How stack traces help in debugging. How exceptions are arranged in an exception class hierarchy. To declare new exception classes. To create chained exceptions that maintain complete stack trace information. Introduction Exception-Handling Overview Example: Divide By Zero Without Exception Handling Example: Handling ArithmeticExceptions and InputMismatchExceptions When to Use Exception Handling Java Exception Hierarchy finally block Stack Unwinding printStackTrace, getStackTrace and getMessage Chained Exceptions Declaring New Exception Types Preconditions and Postconditions Assertions Wrap-Up Introduction Exception – an indication of a problem that occurs during a program’s execution Exception handling – resolving exceptions that may occur so program can continue or terminate gracefully Exception handling enables programmers to create programs that are more robust and fault-tolerant Error-Prevention Tip Exception handling helps improve a program’s fault tolerance. Introduction Examples ArrayIndexOutOfBoundsException – an attempt is .