Chapter 13. Risky Behavior: Exception Handling

image

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 guarantee 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 multiplayer version so you can send your drum loops to another player, kind of like sharing over social media. You’re going to write the whole thing, although you can choose to use Ready-Bake ...

Get Head First Java, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.