0

I am using the following code to try and catch all the errors that can occur for an audioPlayer. However, the very last brace creates a syntax error. Can anyone tell me why?

It says: "Syntax Error, insert '}' to complete ClassBody"

Code:

public void audioPlayer(String path, String fileName){
    //set up MediaPlayer    
    MediaPlayer mp = new MediaPlayer();

    try {
        mp.setDataSource(path+"/"+fileName);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        mp.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mp.start();
}
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Your error would appear to be elsewhere in your class, as I do not see any mis-matched braces here. Also, unless you are going to have different behaviors for the different exceptions, you might consider simply having one `catch` block for `Exception`, from which all those other exceptions inherit. – CommonsWare Jul 15 '12 at 23:58
  • I cannot see a syntax error. Unfortunately the latest Android Developer Tools messed up Eclipse pretty badly. Old errors like syntax errors do not go away even though they're fixed. You could try cleaning your app project (Eclipse menu: Project -> Clean), that usually fixes that problem. – tiguchi Jul 16 '12 at 00:04
  • 1
    Could you at least remove the "todo" comments? They're not relevant. – Dave Newton Jul 16 '12 at 00:06
  • As CommonsWare suggests a single Try/Catch block may be better. Although I much prefer allowing everything to be thrown as far as possible back up the stack to a generic exception handler, and only catching specific exceptions where you really need to. – AW101 Jul 16 '12 at 00:38

2 Answers2

0

This bug have been fixed by the ADT team : http://code.google.com/p/android/issues/detail?id=33985 It should be released soon.

Cleaning the project solve the problem actually.

eyal-lezmy
  • 7,090
  • 3
  • 41
  • 35
0

mp.start() can throw an IllegalStateException that you are not catching or allowing to be thrown.

According to: http://developer.android.com/reference/android/media/MediaPlayer.html#start%28%29

AW101
  • 1,620
  • 14
  • 15