Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence Introduction to Data Persistence Handling an Exception

compiler error in challenge

In this chalenge I always see Bummer! There is a compiler error. Please click on preview to view your syntax errors! and the error is: JavaTester.java:22: error: exception Exception is never thrown in body of corresponding try statement } catch(Exception ex) {

But in my code I wrote catch (Exception e) - not Exception ex so this error is not reffering to my code!

CodeChallenge.java
// assetManager, assetName, and fileToWrite have been initialized elsewhere

try {

InputStream in = assetManager.open(assetName);
FileOutputStream out = new FileOutputStream(fileToWrite);
copyFile(in, out);

} catch (Exception e) {
  e.printStackTrace();
}

2 Answers

There is no error in the question. This is the code I used to solve it. You are supposed to throw a generic Exception which is to be caught.

Try{ //the code given by the question throw new Exception(); }catch(Exception e){ e.printStackTrace(); }

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

problem is that you cannot throw and exception on copyFile method, since you cannot see it.

George Pirchalaishvili yes you can. So are you implying that you only throw exceptions on methods that you can 'see'?

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

Well as I understand that, is that when you declare a method, you can state that it throws the exception, if you do so, exception will be handled no inside the method, but where the method is called (like in this particular example).

What you are referring to is called catching exception not throwing it:

try {
copyFile(in, out);
}catch(Exception e)
{ e.printStackTrace(); 
} 

Problem is, that here you are trying to catch an exception which should be thrown by method copyFile, but if doesnt state in copyFile method that it throws an exception - it should be handled not here, but inside the method itself.

At least thats how I understand the system.

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

I just tried it - it works. But you are catching just new exception, which is not exception from the code itself. It is nice workaround, but still mistake in exercise.

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

This is bugged exercise :) I saw couple other people, who could not finish it.