Chapter 13. Risky Behavior: Exception Handling
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.