0

i have a weird question. i had a quiz in my class today. One portion of the quiz was to find and correct errors in a short piece of code. one of the questions was like this

 class Example {
    public static void main(String[] args) {
        try {
            System.out.println("xyz");
        } catch (Exception e) {
            System.out.println("Exception caught");
        } finally {
            System.out.println("abc");
        }
    }
 }

I thought there was no error in the program but my professor insisted that there was. Can anyone guess what the error is?

5
  • just compiled and run and works for me; there is no compile /run error; maybe he expected public class Example or the fact that you shouldn't catch exceptions Commented Oct 24, 2014 at 14:06
  • Make no sense of using Try and Catch block just to print. Commented Oct 24, 2014 at 14:06
  • 1
    Are you sure it wasn't IOException instead of just Exception? Commented Oct 24, 2014 at 14:07
  • @Mohammad Yes. i'm sure. So no exception? Commented Oct 24, 2014 at 14:15
  • @UsamaKhurshid read my answer below. Commented Oct 24, 2014 at 14:16

4 Answers 4

5

The "error" may be that you don't need to handle any exception here: System.out.println does not specify any checked exception. It could simply be:

public static void main(String[] args) {        
     System.out.println("xyz");        
}

Since the Exception class covers both checked and unchecked exceptions, then if you add a catch block here, in this case you would be handling only unchecked exceptions, which you should not normally handle.

Sign up to request clarification or add additional context in comments.

Comments

1

There is no Error in the Above Program , but also there is no need to put a try{} catch{} ....since you don't use any code that can throw an Exception , for example a risky method like Thread.sleep();

So maybe that’s what your professor meant .

Comments

0

Well, I see nothing that would keep this from compiling, but I do see some problems. To begin with, there are comments indicating the presence of code which is not there. Comments out of sync with code is always a problem. [EDIT: indentation errors have been edited away] And you're catching Exception e, which you really oughtn't to do. You should always catch a specific exception that you expect to encounter, and handle it specifically. Since there's no exception that System.out.println can throw, this would make the whole Exception handling block a problem.

1 Comment

the comments indicate statements. i'll edit the question
0

The following code snippet would throw a compilation error if used with IOException, since System.out.println would never throw an IOException but could throw Exception or Throwable which is its super class.

try {
    System.out.println("xyz");
} catch (IOException e) {
    //simple display error statement here
} finally {
    //simple print statement here
}

2 Comments

can you explain why this code will throw compilation error with 'IOException"?
Unreachable catch block for IOException. This exception is never thrown from the try statement body this is why. :) In other words, System.out.println never throws and IOException.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.