SlideShare a Scribd company logo
Let’s Make A Music MachineLIS4930 © PICWell it won’t look this good 
Why?LIS4930 © PICWe 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 – capable digital instrumentMIDI fileSpeaker
We’ll Start With The BasicsLIS4930 © PICFor 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.Something is wrong, let’s look at this in EclipseBefore 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:
What if you want to call a method that is risky?LIS4930 © PIC1Let’s say you want to call a method in a class that you didn’t write.2That method does something risky, something that might not work at runtime3You need to know that the method you’re calling is risky4You 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
ExceptionsLIS4930 © PICMethods 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:throwsThe Java API tells you when to except an exception
You Must Tell the Compiler You are Using a Risky MethodLIS4930 © PICIn 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 consentCatch blockCatches the exception if it occursTry block
More About ExceptionsLIS4930 © PICAn 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.
Exception HierarchyLIS4930 © PICgetMessage( )printStackTrace( )ThrowableExceptionIOExceptionInterruptedException
We Know How Catching Works, but What About Throwing?LIS4930 © PIC1Risky, exception-throwing code:public void takeRisk( )  throws BadException {	if (abandonAllHope) {		throw new BadException( );	}}2Your code that calls the risky method:public void crossFingers( ) {	try {anObject.takeRisk( );	} catch (BadException ex) {System.out.println(“Aaargh”);ex.printStackTrace( ); 	}}
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)LIS4930 © PIC

More Related Content

PPT
Exception handling
PPTX
Java exception handling
PPTX
Exception handling in JAVA
PPTX
Exception handling in java
PPTX
Exception handling in java
PDF
Java review: try catch
PPTX
Z blue exception
PPTX
Event handling
Exception handling
Java exception handling
Exception handling in JAVA
Exception handling in java
Exception handling in java
Java review: try catch
Z blue exception
Event handling

What's hot (18)

PPTX
Exceptionhandling
PPTX
Exception handling
PPTX
Android app code mediator
PPT
Multi catch statement
PPTX
Exception handling in Python
PDF
Exception Handling in Java
PPTX
Exception handling
PPTX
Chapter 13 exceptional handling
PPT
Exceptions
PPTX
Presentation on-exception-handling
PPTX
7.error management and exception handling
PPTX
exception handling in java
PPTX
Exception handling in java
PPT
Exceptions
PDF
Java unit 11
PPTX
Exception handling in java
PPT
Exception Handling
PPT
Eo gaddis java_chapter_10_5e
Exceptionhandling
Exception handling
Android app code mediator
Multi catch statement
Exception handling in Python
Exception Handling in Java
Exception handling
Chapter 13 exceptional handling
Exceptions
Presentation on-exception-handling
7.error management and exception handling
exception handling in java
Exception handling in java
Exceptions
Java unit 11
Exception handling in java
Exception Handling
Eo gaddis java_chapter_10_5e
Ad
Ad

Similar to 14a exceptions (20)

PPTX
OBJECT ORIENTED PROGRAMMING STRUCU1.pptx
PPT
Exception handling
PPTX
Java programming-Event Handling
PPT
Java: Exception
PPTX
UNIT III 2021R.pptx
PPTX
UNIT III 2021R.pptx
PPTX
Exception Handling
PDF
L12.2 Exception handling.pdf
PPT
Exceptions in java
PPT
Exception handling
PPTX
SUBHASH.pptx
PPT
Exceptions in java
PPT
Java Exception.ppt
PDF
Week12
PDF
Ch-1_5.pdf this is java tutorials for all
PPT
12 exception handling
PPT
12 exception handling
PDF
7_exception.pdf
PPT
Chapter 8 - Exceptions and Assertions Edit summary
OBJECT ORIENTED PROGRAMMING STRUCU1.pptx
Exception handling
Java programming-Event Handling
Java: Exception
UNIT III 2021R.pptx
UNIT III 2021R.pptx
Exception Handling
L12.2 Exception handling.pdf
Exceptions in java
Exception handling
SUBHASH.pptx
Exceptions in java
Java Exception.ppt
Week12
Ch-1_5.pdf this is java tutorials for all
12 exception handling
12 exception handling
7_exception.pdf
Chapter 8 - Exceptions and Assertions Edit summary

More from Program in Interdisciplinary Computing (20)

14a exceptions

  • 1. Let’s Make A Music MachineLIS4930 © PICWell it won’t look this good 
  • 2. Why?LIS4930 © PICWe 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 – capable digital instrumentMIDI fileSpeaker
  • 3. We’ll Start With The BasicsLIS4930 © PICFor 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.Something is wrong, let’s look at this in EclipseBefore 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:
  • 4. What if you want to call a method that is risky?LIS4930 © PIC1Let’s say you want to call a method in a class that you didn’t write.2That method does something risky, something that might not work at runtime3You need to know that the method you’re calling is risky4You 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
  • 5. ExceptionsLIS4930 © PICMethods 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:throwsThe Java API tells you when to except an exception
  • 6. You Must Tell the Compiler You are Using a Risky MethodLIS4930 © PICIn 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 consentCatch blockCatches the exception if it occursTry block
  • 7. More About ExceptionsLIS4930 © PICAn 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.
  • 8. Exception HierarchyLIS4930 © PICgetMessage( )printStackTrace( )ThrowableExceptionIOExceptionInterruptedException
  • 9. We Know How Catching Works, but What About Throwing?LIS4930 © PIC1Risky, exception-throwing code:public void takeRisk( ) throws BadException { if (abandonAllHope) { throw new BadException( ); }}2Your code that calls the risky method:public void crossFingers( ) { try {anObject.takeRisk( ); } catch (BadException ex) {System.out.println(“Aaargh”);ex.printStackTrace( ); }}
  • 10. 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)LIS4930 © PIC