threewire
threewire

Reputation: 477

IO exception even after including try catch block

The following piece of code is creating error

Activity context;
context = new Activity();
try {
    InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
    //SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());

}catch (FileNotFoundException e) {
    Log.e(LOG_TAG, "File not found for recording ", e);
}

Error:

Error:(255, 87) error: unreported exception IOException; must be caught or declared to be thrown
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

Even after I included the try,catch blocks why is it creating an error?

Upvotes: 1

Views: 131

Answers (3)

Dishonered
Dishonered

Reputation: 8841

Try this, IOException is the parent of FileNotFoundException. So catching the IOException will ensure you catch any exception that is a subclass of IOException.

Activity context;
context = new Activity();
try {

    InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
    //SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());

}catch (IOException e) {
    Log.e(LOG_TAG, "File not found for recording ", e);
}

Upvotes: 2

Subin Babu
Subin Babu

Reputation: 1555

You should include the following to avoid the crash,

        try {
            InputStream configAsset =getApplication().getAssets().open("MyConfig.cfg");               
        }catch (FileNotFoundException e) {
            Log.e("ddsa", "File not found for recording ", e);
        } catch (IOException e) {
            Log.e("ddsa", "IOException  ", e);
            e.printStackTrace();
        } 

Also the context you set not work well for me that's why getApplication()

Upvotes: 0

Koustuv Ganguly
Koustuv Ganguly

Reputation: 899

Change your code like this :

BEFORE:

Activity context;
        context = new Activity();
        try {
            InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
            //SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());

        }catch (FileNotFoundException e) {
            Log.e(LOG_TAG, "File not found for recording ", e);
        }

AFTER:

Activity context;
        context = new Activity();
        try {
            InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
            //SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());

        }catch (IOException e) {
            Log.e(LOG_TAG, "File not found for recording ", e);
        }

Upvotes: 2

Related Questions