aquemini001
asked on
What's wrong with this code? What does the output look like?
try{
System.out.print(“Hello world “);
}
finally {
System.out.println(“Finall y Executing “);
}
What should my output look like? this wouldn't compile on my machine.
System.out.print(“Hello world “);
}
finally {
System.out.println(“Finall
}
What should my output look like? this wouldn't compile on my machine.
http://www.site.uottawa.ca:4321/java/index.html#try-catch-finallystatement
http://www.churchillobjects.com/c/11012c.html
http://www.janeg.ca/scjp/flow/try.html
try{
System.out.print(“Hello world “);
}
}
catch(Exception e){
}
finally{
System.out.println(“Finall
}
output:
Hello world
Finally Executing
have a look at beginners site...
http://www.janeg.ca/scjp/language.html
http://www.janeg.ca/scjp/language.html
there should not be a try block without catch....
place a catch block after the try...like:
try{
System.out.println("Hello World");
}
catch(Exception e) { // one or more catch block is a MUST.
System.out.println("Except ion in : " +e);
}
finally { // one or none....OPTIONAL
System.out.println("Finall y Executing");
}
place a catch block after the try...like:
try{
System.out.println("Hello World");
}
catch(Exception e) { // one or more catch block is a MUST.
System.out.println("Except
}
finally { // one or none....OPTIONAL
System.out.println("Finall
}
here also, the same repeated mahesh.... ;-) ...
ok no problem..... :-)
Actually - its perfectly legal to have a try-finally (without a catch).
For example :
try
{
System.out.println("hello" );
}
finally
{
System.out.println("world" );
}
Will print out :
Hello
World
This is useful if you want to definitively do some sort of cleanup behavior (e.g. close a database connection), even in the face of an exception, but you don't want to handle the exception (e.g. it gets propagated out of the method, to the caller).
For example :
try
{
System.out.println("hello"
}
finally
{
System.out.println("world"
}
Will print out :
Hello
World
This is useful if you want to definitively do some sort of cleanup behavior (e.g. close a database connection), even in the face of an exception, but you don't want to handle the exception (e.g. it gets propagated out of the method, to the caller).
yeah, some format for try catch is explained in the links i posted....
aquemini001, your program with a try - finally is completely legal according to the JAVA 2 standards.
It doesn't compile because you should surround strings with "
that is: ordinary double quotes and not the wordprocessor characters in your example.
Om my machine, W2K JAVA 2 1.4.2_04 it compiles and works OK.
;JOOP!
It doesn't compile because you should surround strings with "
that is: ordinary double quotes and not the wordprocessor characters in your example.
Om my machine, W2K JAVA 2 1.4.2_04 it compiles and works OK.
;JOOP!
It is also possible that some other part of your program is giving compilation errors. Could you post the full code?
ASKER
Some say i must have a "catch" after a try. Some say i don't. If it works both ways, which one is better? or is it just a matter of preference. Which was is more standard? to ALWAYS have a catch statement after a try? Can you u just have an empty catch statement followed by the "finally"?
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that helped, thanks!
Yep....sorry for my stupid post...
lhankins comment is cool...
lhankins comment is cool...
ASKER
no, your answers are good too, you've answered a couple of my other questons. thanks
always the format is
try{
...
}
catch(Exception e){
....
}
finally{
......
}