+ All Categories
Home > Documents > 14a exceptions

14a exceptions

Date post: 06-Dec-2014
Category:
Upload: program-in-interdisciplinary-computing
View: 456 times
Download: 4 times
Share this document with a friend
Description:
 
10
Let’s Make A Music Machine LIS4930 © PIC Well it won’t look this good
Transcript
Page 1: 14a exceptions

LIS4930 © PIC

Let’s Make A Music Machine

Well it won’t look this good

Page 2: 14a exceptions

LIS4930 © PIC

Why?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.

The JavaSound API is a good source of risky code, so that is why we are building a sound machine. 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.

MIDI file

MIDI – capable digital

instrument Speaker

Page 3: 14a exceptions

LIS4930 © PIC

We’ll Start With The Basics

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.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.The Sequencer class is in the javax.sound.midi package. So now, let’s get started with a Sequencer object: Something is

wrong, let’s look at this in Eclipse

Page 4: 14a exceptions

LIS4930 © PIC

What if you want to call a method that is risky?

Let’s say you want to call a method in a class that you didn’t write.

1

2 That method does something risky, something that might not work at runtime

3 You need to know that the method you’re calling is risky

4 You then write code that can handle the failure if it does happen. You need to be prepared, just in case.

Let’s look at the getSequencer method in the Java API

Page 5: 14a exceptions

LIS4930 © PIC

ExceptionsMethods 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.The benefit of throwing an exception is that you can look out for it, and even catch it and recover from it.

You can tell if some code gives exceptions by looking for the keyword:

throws

The Java API tells you when to except an exception

Page 6: 14a exceptions

LIS4930 © PIC

You Must Tell the Compiler You are Using a Risky Method

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.

Something is wrong – I haven’t given my consent

Try block

Catch blockCatches the exception if it occurs

Page 7: 14a exceptions

LIS4930 © PIC

More About ExceptionsAn exception is an OBJECT….. of type Exception.

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.

try { // do risky

thing} catch (Exception ex) {

// try to recover}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.

Page 8: 14a exceptions

LIS4930 © PIC

Exception HierarchyThrowable

Exception

IOException

InterruptedException

getMessage( )printStackTrace( )

Page 9: 14a exceptions

LIS4930 © PIC

We Know How Catching Works, but What About Throwing?

1 Risky, exception-throwing code: public void takeRisk( ) throws BadException {

if (abandonAllHope) {throw new

BadException( );}

}

2 Your code that calls the risky method:

public void crossFingers( ) {try {

anObject.takeRisk( );} catch (BadException ex) {

System.out.println(“Aaargh”);ex.printStackTrace( );

}}

Page 10: 14a exceptions

LIS4930 © PIC

The Compiler Guarantees:

If you throw an exception in your code you must declare it using the throws keyword in your method declaration.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.It will NOT catch RuntimeExceptions – because those are errors in your code, and not exceptional circumstances. (Read page 324 on this)


Recommended