0

Just wondering if this is a correct structure to use with try and exceptions:

MediaPlayer.OnCompletionListener videoViewCompletionListener = new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) throws IllegalStateException {
        try {
            mp.stop();
            mp.release();
            videoView.stopPlayback();
        } finally {
            toastDisplay.cancel();
            toastDisplay = Toast.makeText(MainActivity.this, "Completed Video", Toast.LENGTH_LONG);
            toastDisplay.show();
        }
    }
};

Will it crash my app or not when i do it this way, incase it throws the IllegalStateException in this example?

1 Answers1

0

You state in your declaration that your method throws an IllegalStateException. However, you are never raising an exception for the caller to handle. If the exception you want to throw is caused by a MediaPlayer method, you need to have a catch block. In that, just throw the exception up for the caller to handle.

enter image description here

Also, Exception handling : throw, throws and Throwable is a good read.

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • So its not possible to make it like i want to do it and ignore the exception? Do you mind making an example out of my example? – Mohamed Allouch Mar 06 '15 at 04:26
  • Yes, if you want that the app continue doing its stuff you can surround with the try only the method that may throw an exception. Then, the execution will be CODE, CODE, TRY, (if exception CATCH), CODE CODE. Sequential as ever. You also will have to add some null control after the try/catch because of the exception maybe thrown – Joaquin Iurchuk Mar 06 '15 at 04:40
  • @MohamedAllouch well, joaquin said it (: – An SO User Mar 06 '15 at 04:47
  • So in this case my example is correct? It will not crash incase the exception is thrown? Sorry my english isnt that good. – Mohamed Allouch Mar 06 '15 at 04:48
  • @MohamedAllouch In **your** case, if a exception is generated, it will go to the finally block without crashing. If you want to do something when an exception is thrown, like show a message or log the cause, you will need a `catch` block :) – An SO User Mar 06 '15 at 04:58
  • Awesome, thats what i needed to hear :) Do i need to have the `throws IllegalStateException` that i have added in the end of the public void or is it not necessary to have in my case? – Mohamed Allouch Mar 06 '15 at 05:06
  • @MohamedAllouch Do you intend to throw the exception up to the caller? If not, just add a catch block and remove the `throws`. – An SO User Mar 06 '15 at 05:29
  • I dont want to use a catch block, just try { } finally { }. Thank you for the help guys. – Mohamed Allouch Mar 06 '15 at 08:16
  • Sorry im new here, this is also my first app. How do i accept the answer? ^^ – Mohamed Allouch Mar 07 '15 at 11:39
  • @MohamedAllouch welcome to SO :) Click on the tick mark above, to the left side of my answer. – An SO User Mar 07 '15 at 11:48
  • Thank you :) unfortunately my account have been banned from asking new questions :( I am new with all this, give me a break SO :( Have a nice day my friend – Mohamed Allouch Mar 07 '15 at 11:52