14a exceptions

381 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
381
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

14a exceptions

  1. 1. Let’s Make A Music Machine<br />LIS4930 © PIC<br />Well it won’t look this good <br />
  2. 2. Why?<br />LIS4930 © PIC<br />We are going to learn about working with risky code. The code we 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.<br />The JavaSound API is a good source of risky code, so that is why we are building a sound machine. <br />JavaSound is a collection of classes and interfaces in Java. JavaSound is split into two parts: MIDI and Sampled. We will only use MIDI. MIDI stands for Musical Instrument Digital Interface – it is like sheet music for digital devices to make audible sounds. <br />MIDI – capable digital instrument<br />MIDI file<br />Speaker<br />
  3. 3. We’ll Start With The Basics<br />LIS4930 © PIC<br />For our example, we use only the built-in, software-only, instrument that you get with Java. It’s called a synthesizer because it creates sound.<br />Something is wrong, let’s look at this in Eclipse<br />Before we can get any sound to play, we need a Sequencer object. The sequencer is the object that takes all the MIDI data and sends it to the right instruments. It’s the thing that plays the music.<br />The Sequencer class is in the javax.sound.midi package. So now, let’s get started with a Sequencer object: <br />
  4. 4. What if you want to call a method that is risky?<br />LIS4930 © PIC<br />1<br />Let’s say you want to call a method in a class that you didn’t write.<br />2<br />That method does something risky, something that might not work at runtime<br />3<br />You need to know that the method you’re calling is risky<br />4<br />You then write code that can handle the failure if it does happen. You need to be prepared, just in case.<br />Let’s look at the getSequencer method in the Java API<br />
  5. 5. Exceptions<br />LIS4930 © PIC<br />Methods in Java use exceptions to tell the calling code, “Something bad happened. I failed.” When using risky code, it will usually throw exceptions if it fails.<br />The benefit of throwing an exception is that you can look out for it, and even catch it and recover from it.<br />You can tell if some code gives exceptions by looking for the keyword:<br />throws<br />The Java API tells you when to except an exception<br />
  6. 6. You Must Tell the Compiler You are Using a Risky Method<br />LIS4930 © PIC<br />In Java you MUST consent to using a risky method by wrapping the method call in a try/catch block before your program will run.<br />Something is wrong – I haven’t given my consent<br />Catch blockCatches the exception if it occurs<br />Try block<br />
  7. 7. More About Exceptions<br />LIS4930 © PIC<br />An exception is an OBJECT….. of type Exception.<br />Because an Exception is an object, what you catch is an object. In the following code, the catch argument is declared as type Exception, and the parameter variable is ex.<br />try { <br /> // do risky thing<br />} catch (Exception ex) {<br /> // try to recover<br />}<br />What you write in a catch block depends on the exception that was thrown. For example, if a server is down you might use the catch block to try another server. If the file isn’t there, you might ask the user for help finding it.<br />
  8. 8. Exception Hierarchy<br />LIS4930 © PIC<br />getMessage( )<br />printStackTrace( )<br />Throwable<br />Exception<br />IOException<br />InterruptedException<br />
  9. 9. We Know How Catching Works, but What About Throwing?<br />LIS4930 © PIC<br />1<br />Risky, exception-throwing code:<br />public void takeRisk( ) throws BadException {<br /> if (abandonAllHope) {<br /> throw new BadException( );<br /> }<br />}<br />2<br />Your code that calls the risky method:<br />public void crossFingers( ) {<br /> try {<br />anObject.takeRisk( );<br /> } catch (BadException ex) {<br />System.out.println(“Aaargh”);<br />ex.printStackTrace( );<br /> }<br />}<br />
  10. 10. The Compiler Guarantees:<br />If you throw an exception in your code you must declare it using the throws keyword in your method declaration.<br />If you call a method that throws an exception (in other words, a method that declares it throws an exception), you must acknowledge that you’re aware of the exception possibility.<br />It will NOT catch RuntimeExceptions – because those are errors in your code, and not exceptional circumstances. (Read page 324 on this)<br />LIS4930 © PIC<br />

×