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

Java Java Objects (Retired) Harnessing the Power of Objects Exceptions

Steven Wheeler
Steven Wheeler
2,330 Points

Why does "println" not work but "printf" does in "Exceptions" lesson?

Hey there!

Out of interest why does "printf" work here (3rd line): "catch (IllegalArgumentException iae) { System.out.println("Woah there!"); System.out.printf("There was an error: %s", iae.getMessage()); }"

but if we were to use "println" it goes crazy?

Thanks! :)

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

printf is for printing formatted text and takes additional parameters. println is for printing lines of text and does not do substitution.

Make sense?

Hi Steven Wheeler,

System.out.println("")

can only print an unformatted string. Thus, if you would leave the %s out of the String everything would work as expected.

System.out.printf("") // The f in printf stands for formatted

this version can print regular strings and formatted strings e.g. %s, %d and so on. If you put %s in the string you are using the formatted version and replace the placeholder %s with some string.

I hope that cleared things up a little bit for you.

Oh, just saw that Craig was faster than me and already answered your question :-)

Steven Wheeler
Steven Wheeler
2,330 Points

Excellent, makes sense! Thanks guys!