O'Reilly logo

Head First Java, 2nd Edition by Bert Bates, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11. Exception Handling: Risky Behavior

image with no caption

Stuff happens. The file isn’t there. The server is down. No matter how good a programmer you are, you can’t control everything. Things can go wrong. Very wrong. When you write a risky method, you need code to handle the bad things that might happen. But how do you know when a method is risky? And where do you put the code to handle the exceptional situation? So far in this book, we haven’t really taken any risks. We’ve certainly had things go wrong at runtime, but the problems were mostly flaws in our own code. Bugs. And those we should fix at development time. No, the problem-handling code we’re talking about here is for code that you can’t guaranatee will work at runtime. Code that expects the file to be in the right directory, the server to be running, or the Thread to stay asleep. And we have to do this now. Because in this chapter, we’re going to build something that uses the risky JavaSound API. We’re going to build a MIDI Music Player.

Let’s make a Music Machine

Over the next three chapters, we’ll build a few different sound applications, including a BeatBox Drum Machine. In fact, before the book is done, we’ll have a multi-player version so you can send your drum loops to another player, kind of like a chat room. You’re going to write the whole thing, although you can choose to use Ready-bake code for the GUI parts. OK, so not every ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required