• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

One more on exceptions

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given this code snippet:
try {
tryThis() ;
return;
} catch (IOException x1) {
System.out.println("exception 1") ;
return;
} catch (Exception x2) {
System.out.println("exception 2") ;
return;
} finally {
System.out.println("finally") ;
}
What will appear in the standard output if tryThis() throws a NumberFormatException?
Select the one right answer.
a.Nothing
b."exception 1", followed by "finally"
c."exception 2", followed by "finally"
d."exception 1"
e."exception 2"

since the try block does not throw the checked exception(IOException) given in the catch block this will give compile error right? but there is no compile error option. in this case should we select a or should it b left unanswered ?

Thanks in advance
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that this is an error in the problem, and that you should not reply 'a'.
(Answer 'a' probably refers to the return in the try block.)
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NumberFormatException is subclasses of IllegalArgumentException -> RuntimeException -> Exception.

So when NumberFormatException it will be caught inside catch (Exception x2) block. As NumberFormatException subclass of RuntimeException there wont be any compilation error.

So the answer is:
c."exception 2", followed by "finally"

Note: try block can throw any thing which is sub class of Throwable(both checked, unchecked exceptions). Even you can throw Error but which strictly to be avoided.
[ January 17, 2006: Message edited by: KJ Reddy ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KJ,
did you actually try to compile it ?
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
KJ,
did you actually try to compile it ?



Satou, as the code is not complete, we cant compile the code snippet. If you have queries you can try writing a class with tryThis() method and throw NumberFormatException from inside that method. As NumberFormatException is RuntimeException we dont even declare it in method throws signature.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to insist, but could you try it yourself ?
I mean, I've tried, and it does not compile, so I must have gone wrong somewhere.

If you feel lazy to write your own, try mine
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to add that if tryThis would throw IOException too, it would compile.
But this was not stated in the problem above.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a small program using the code snippet given.
The program follows

import java.io.*;
class CheckedException
{
static void tryThis()throws NumberFormatException
{
throw new NumberFormatException();
}
public static void main(String[] args)
{
try {
tryThis() ;
return;
} catch (IOException x1) {
System.out.println("exception 1") ;
return;
} catch (Exception x2) {
System.out.println("exception 2") ;
return;
} finally {
System.out.println("finally") ;
}
}
}

Now when I tried to compile it, I got a compile time error saying

CheckedException.java:13: exception java.io.IOException is never thrown in body of corresponding try statement
} catch (IOException x1){ ^

I feel that the options for the question stated is wrong.
Hope this helps !!!

Cheers,
Sandeep
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same for me, and probably same for Gauri too.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
I'd like to add that if tryThis would throw IOException too, it would compile.
But this was not stated in the problem above.



Satou, even though the problem is not stated that you can assume that. This kind of questions may not be straight forward and may not give each and every detail.

I just modified your code so that tryThis() method throws IOException. And in your code you dont need to add throws NumberFormatException as it is RuntimeException.

Dont look some logic in the following code. I just want to display what happens if NumberFormatException is thrown.



import java.io.*;
public class Excep {
public static String s = "";
public static void main(String[] args) {
try {
tryThis() ;
return;
} catch (java.io.IOException x1) {
System.out.println("exception 1") ;
return;
} catch (Exception x2) {
System.out.println("exception 2") ;
return;
} finally {
System.out.println("finally") ;
}
}

public static void tryThis() throws IOException {

if(s.equals("IO")){
throw new IOException();
}
else {
throw new NumberFormatException();
}
}
}

 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandeep MP:
I wrote a small program using the code snippet given.
The program follows


Now when I tried to compile it, I got a compile time error saying

CheckedException.java:13: exception java.io.IOException is never thrown in body of corresponding try statement
} catch (IOException x1){ ^

I feel that the options for the question stated is wrong.
Hope this helps !!!

Cheers,
Sandeep



Sandeep thats the difference between checked and unchecked exceptions. When catch checked exceptions(IOException) compiler checks for validity of your catch statment that are there any chances for getting this exception in your code. Where as if you catch unchecked exceptions(NumberFormatException) compiler wont check the validity.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic