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

Try and catch

i have this code :

try { dispenser.load(400); System.out.println(""); } catch(IllegalArgumentException iae){ System.out.println("WHOA THERE!"); System.out.printf("The error was: %s\n" , iae.getMessage()); }

is it fine ? or should i put some sentence in the System.out.println(""); ? or it is fine ? is there any problem w this ? it will cause any problem ? please let me know. thanks in advance guys.

1 Answer

Hi Kevin.

No there is no problem in doing that. Calling 2 methods instead of 1 in this case has a minimal footprint, and there will be nothing wrong with it, in fact some programmers, usually may devide the string to be printed in multiple methods for readability purposes.

Anyway if you are really worried with performance and efficiency you should declare a class variable like:

class SomeClass{

private static String errorText = "WHOA THERE!\nThe error was: %s\n";

public void someMethod(){

try{
//do stuff
catch(Exception e){
printf(SomeClass.errorText , e.getMessage());
}

}

}

In this way you save runtime processing time of the String and it's only processed once when the first load of the class is called on your program, of course it will still spend some time to concatenate the getMessage() String.

sorry for my question i thought it will appear the same as i typed it , and thank you you understand it , i got your point and you answered my question. Thanks mate :)