MIDI Devices


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: MIDI Devices

  1. #1
    Join Date
    Oct 2005
    Posts
    3

    Devices - Query

    Hi,

    Ive got this program which lists all MIDI devices connected to the system, and asks the user to select one of them, and then will open the device if its available.

    Code:
    import javax.swing.*;
    import javax.sound.java.*;
    
    public class MidiDeviceOpener {
    
        public static void main (String args[]) {
          MidiDevice.Info [] deviceList=MidiSystem.getMidiDeviceInfo ();
    			
          MidiDevice.Info deviceInfo =
              (MidiDevice.Info)
                 JOptionPane.showInputDialog (
    	  null,
    	  "Select MIDI Device",
    	  "Program Name",
    	JOptionPane.QUESTION_MESSAGE,
    	 null,
    	deviceList,
    	null);
    			
    System.out.println ("Select Device = " + deviceInfo);
    if (deviceInfo !=null) {
    
    try {
       MidiDevice device = MidiSystem.getMidiDevice (deviceInfo);
       device.open();
       
       // Actions to operate the device go here
      device.close ();
    }
    catch (MidiUnavailableException e) {
      System.out.println ("Device not available");
               }
            }
        }
    }
    I now wish to modify this program so it throws a MidiUnavailableExpeception and change the println () call so that it produces a more useful error message.

    Is this how i would make it throw a MidiUnavilableException?, if so where would i put it in the code:

    Code:
    try {
      MidiDevice device = MidiSystem.getMidiDevice (deviceInfo);
      device.open ();
    }
    catch (MidiUnavailableException e) {
      System.out.println ("Unable to open device");
    If i decide not to handle a particular exception inside a method. What syntatical changes must be made to the method heading? And what steps must be taken inside the calling method to ensure that the exception is still correctly handled?


    Thanks
    Last edited by DanielJones1980; 10-23-2005 at 10:23 AM.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If i decide not to handle a particular exception inside a method
    Then the method header must be changed so it signals that the exception
    may be thrown:
    Code:
    public void aMethod () throws SomeException {
      .
      .
    }
    Invoking this method requires that the invokation is inside a try/catch for
    the particular exception, or that the invokation appears inside a method
    that declares that this exception may be thrown.

    How you handle an exception relies on the scenario but there are a couple of
    practical rules to follow:

    Never use an exception for handling a situation that is not an exception (the
    distinction between error and exceptin may be vague though).

    Never "mute" an exception. It may bury the bug(s) silently and keep you
    searching all over the code, so don't wite code like this, unless you are
    101% certain that its OK.

    try {
    // some code
    } catch (SomeException ex) {}
    eschew obfuscation

  3. #3
    Join Date
    Oct 2005
    Posts
    3
    thanks for the explanation sjalle.

    Where would i put the MidiUnavilableException code then in my MidiDeviceOpener program?

    Thanks

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    It depends on where you want to handle it....
    eschew obfuscation

  5. #5
    Join Date
    Oct 2005
    Posts
    3
    I think it makes sense to put this call in the same try block as the call to getMidiDevice ():

    Am i correct? If so can would you be able write out the code for me (its all above) because im getting a couple of errors.

Similar Threads

  1. JOptionPane focus
    By doredson in forum Java
    Replies: 5
    Last Post: 03-14-2007, 02:07 PM
  2. JOptionPane single string input w/four values
    By JavaBeanie in forum Java
    Replies: 1
    Last Post: 10-23-2005, 01:16 AM
  3. JRadioButtons and JOptionPane
    By Wizard1988 in forum Java
    Replies: 3
    Last Post: 08-31-2005, 12:24 AM
  4. JOptionPane and if (a.equals.. problem
    By Arkie_student in forum Java
    Replies: 1
    Last Post: 06-08-2005, 09:48 AM
  5. question about JOptionPane
    By Harry in forum Java
    Replies: 3
    Last Post: 06-25-2000, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center