Solved

What's wrong with this code? What does the output look like?

Posted on 2004-05-01
16
176 Views
Last Modified: 2010-03-31
try{
System.out.print(“Hello world “);
}
finally {
System.out.println(“Finally Executing “);
}

What should my output look like? this wouldn't compile on my machine.
0
Comment
Question by:aquemini001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +3
16 Comments
 
LVL 7

Expert Comment

by:maheshexp
ID: 10969964

always the format is
try{
    ...
}
catch(Exception e){
    ....
}
finally{
......
}
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10969969

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(“Finally Executing “);
}

output:
Hello world
Finally Executing
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10969972
have a look at beginners site...
http://www.janeg.ca/scjp/language.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 32

Expert Comment

by:ldbkutty
ID: 10969975
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("Exception in : " +e);
}

finally {       // one or none....OPTIONAL
System.out.println("Finally Executing");
}
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 10970025
here also, the same repeated mahesh.... ;-) ...
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970069
ok no problem..... :-)
0
 
LVL 7

Expert Comment

by:lhankins
ID: 10970103
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).
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970120
yeah, some format for try catch is explained in the links i posted....
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10970553
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!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10971252
It is also possible that some other part of your program is giving compilation errors. Could you post the full code?
0
 

Author Comment

by:aquemini001
ID: 10972979
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
0
 
LVL 7

Accepted Solution

by:
lhankins earned 20 total points
ID: 10973038
Put a catch statement after a try if you can handle the exception or if you don't want it to propagate out of the method.

For example:

   void someFn()
   {  
      try
      {
         // some logic
      }
      catch (Exception e)
      {
          // log the exception
          // recover from the exception
      }
      finally
      {
         // cleanup any resources allocated during the try phase
      }
   }

If you can't handle the exception and you want it to propagate to calling code, you can just skip the catch statement (and add the exception to the method's throws clause).   This means the Exception will be propagated all the way back to the code which called your method.

   void someFn() throws Exception
   {  
      try
      {
         // some logic
      }
      finally
      {
         // cleanup any resources allocated during the try phase
      }
   }

Either way, the finally is still important, because code in the finally block is guaranteed to execute, regardless of whether an exception is thrown or not.

0
 

Author Comment

by:aquemini001
ID: 10973099
that helped, thanks!
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 10973778
Yep....sorry for my stupid post...

lhankins comment is cool...
0
 

Author Comment

by:aquemini001
ID: 10974404
no, your answers are good too, you've answered a couple of my other questons. thanks
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question