Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 9. Polymorphism, Dynamic Typing,... > Exception Handling Using @try

Exception Handling Using @try

Good programming practice dictates that you try to anticipate problems that can occur in your program. You can do this by testing for conditions that could cause a program to terminate abnormally and handling these situations, perhaps by logging a message and gracefully terminating the program or taking some other corrective action. For example, you saw earlier in this chapter how you can test to see if an object responds to a particular message. In the case of error avoidance, performing this test while the program is executing can enable you to avoid sending an unrecognized message to an object. When an attempt is made to send such an unrecognized message, your program will typically terminate immediately by throwing what’s known as an exception.

Take a look at Program 9.4. We have no method called noSuchMethod defined in the Fraction class. When you compile the program, you will get warning messages to that effect.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial