SlideShare a Scribd company logo
1 of 10
Let’s Make A Music Machine LIS4930 © PIC Well it won’t look this good 
Why? LIS4930 © PIC 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 – capable digital instrument MIDI file Speaker
We’ll Start With The Basics LIS4930 © PIC 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. Something is wrong, let’s look at this in Eclipse 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:
What if you want to call a method that is risky? LIS4930 © PIC 1 Let’s say you want to call a method in a class that you didn’t write. 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
Exceptions LIS4930 © PIC 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. 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
You Must Tell the Compiler You are Using a Risky Method LIS4930 © PIC 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 Catch blockCatches the exception if it occurs Try block
More About Exceptions LIS4930 © PIC An 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 Hierarchy LIS4930 © PIC getMessage( ) printStackTrace( ) Throwable Exception IOException InterruptedException
We Know How Catching Works, but What About Throwing? LIS4930 © PIC 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( );  	} }
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

What's hot

Exceptionhandling
ExceptionhandlingExceptionhandling
ExceptionhandlingNuha Noor
 
Android app code mediator
Android app code mediatorAndroid app code mediator
Android app code mediatorFatimaYousif11
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statementmyrajendra
 
Exception handling in Python
Exception handling in PythonException handling in Python
Exception handling in PythonAdnan Siddiqi
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in JavaJava2Blog
 
Exception handling
Exception handlingException handling
Exception handlingMinal Maniar
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handlingDeepak Sharma
 
exception handling in java
exception handling in javaexception handling in java
exception handling in javaAbinaya B
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapooja kumari
 
Exceptions
ExceptionsExceptions
Exceptionsmotthu18
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 
Eo gaddis java_chapter_10_5e
Eo gaddis java_chapter_10_5eEo gaddis java_chapter_10_5e
Eo gaddis java_chapter_10_5eGina Bullock
 

What's hot (18)

Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exception handling
Exception handlingException handling
Exception handling
 
Android app code mediator
Android app code mediatorAndroid app code mediator
Android app code mediator
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statement
 
Exception handling in Python
Exception handling in PythonException handling in Python
Exception handling in Python
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Exception handling
Exception handlingException handling
Exception handling
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
 
Exceptions
ExceptionsExceptions
Exceptions
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handling
 
exception handling in java
exception handling in javaexception handling in java
exception handling in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exceptions
ExceptionsExceptions
Exceptions
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Eo gaddis java_chapter_10_5e
Eo gaddis java_chapter_10_5eEo gaddis java_chapter_10_5e
Eo gaddis java_chapter_10_5e
 

Viewers also liked (9)

06a methods original
06a methods original06a methods original
06a methods original
 
12 abstract classes
12 abstract classes12 abstract classes
12 abstract classes
 
Sdlc
SdlcSdlc
Sdlc
 
Mysocial
MysocialMysocial
Mysocial
 
04 variables
04 variables04 variables
04 variables
 
15a gui
15a gui15a gui
15a gui
 
03 objects
03 objects03 objects
03 objects
 
07 java api and inheritance
07 java api and inheritance07 java api and inheritance
07 java api and inheritance
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 

Similar to 14a exceptions

【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)Unity Technologies Japan K.K.
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJAYAPRIYAR7
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in JavaAnkit Rai
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event HandlingJava Programming
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaAmbigaMurugesan
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++Deepak Tathe
 
How secure is your code?
How secure is your code?How secure is your code?
How secure is your code?Mikee Franklin
 
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...Felipe Prado
 
Dark Side of iOS [SmartDevCon 2013]
Dark Side of iOS [SmartDevCon 2013]Dark Side of iOS [SmartDevCon 2013]
Dark Side of iOS [SmartDevCon 2013]Kuba Břečka
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 

Similar to 14a exceptions (20)

Introduction to Exception
Introduction to ExceptionIntroduction to Exception
Introduction to Exception
 
Java Exceptions Handling
Java Exceptions Handling Java Exceptions Handling
Java Exceptions Handling
 
Java Exceptions
Java ExceptionsJava Exceptions
Java Exceptions
 
Java exception
Java exception Java exception
Java exception
 
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
 
Exception handling
Exception handlingException handling
Exception handling
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.ppt
 
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event Handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Py-Slides-9.ppt
Py-Slides-9.pptPy-Slides-9.ppt
Py-Slides-9.ppt
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
How secure is your code?
How secure is your code?How secure is your code?
How secure is your code?
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
DEF CON 27 - workshop ANTHONY ROSE - introduction to amsi bypasses and sandbo...
 
Dark Side of iOS [SmartDevCon 2013]
Dark Side of iOS [SmartDevCon 2013]Dark Side of iOS [SmartDevCon 2013]
Dark Side of iOS [SmartDevCon 2013]
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 

More from Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Database basics
Database basicsDatabase basics
Database basics
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Xhtml
XhtmlXhtml
Xhtml
 
Webdev
WebdevWebdev
Webdev
 
Web architecture
Web architectureWeb architecture
Web architecture
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Html5
Html5Html5
Html5
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
Javascript2
Javascript2Javascript2
Javascript2
 
11 polymorphism
11 polymorphism11 polymorphism
11 polymorphism
 
13 interfaces
13 interfaces13 interfaces
13 interfaces
 
15b more gui
15b more gui15b more gui
15b more gui
 

14a exceptions

  • 1. Let’s Make A Music Machine LIS4930 © PIC Well it won’t look this good 
  • 2. Why? LIS4930 © PIC 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 – capable digital instrument MIDI file Speaker
  • 3. We’ll Start With The Basics LIS4930 © PIC 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. Something is wrong, let’s look at this in Eclipse 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:
  • 4. What if you want to call a method that is risky? LIS4930 © PIC 1 Let’s say you want to call a method in a class that you didn’t write. 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
  • 5. Exceptions LIS4930 © PIC 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. 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
  • 6. You Must Tell the Compiler You are Using a Risky Method LIS4930 © PIC 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 Catch blockCatches the exception if it occurs Try block
  • 7. More About Exceptions LIS4930 © PIC An 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 Hierarchy LIS4930 © PIC getMessage( ) printStackTrace( ) Throwable Exception IOException InterruptedException
  • 9. We Know How Catching Works, but What About Throwing? LIS4930 © PIC 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( ); } }
  • 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