• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Exception Q2

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class D
{
static void test( ) throws NullPointerException{
}
public static void main(String[] args) throws Exception
{
try{
test( );
System.out.println("message1");
}
catch(NullPointerException e){
System.out.println("message2");

}
catch(Exception e) {
System.out.println("message3");
throw e;
}
finally{
System.out.println("message4");
}
System.out.println("message5");
}
}
//the answer is message2,message4,
but the output is message1,message4,message5 when i run it. but why ? I am confused .please rectify
the answer . I am very grateful to you for your
reason!!
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erico,
Are you sure that you didn't miss some thing in this post? If yes then output is
message1
message4
message5
Because no exception is getting thrown from test method.
Had there been a NullPointerException thrown from test method with in the following code
static void test( ) throws NullPointerException{
D d=null;
d.toString();
}
your output would be
message2
message4
message5
And if you have code some thing like this for test method then
static void test( ) throws NullPointerException{
int x=30,y=0;
int z=x/y;
}
message3
message4
run time error
might be your outputs.
Hope this helps you..
 
Erico Doon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you ,Sarma Lolla!!
but I still confused !!
quote:
And if you have code some thing like this for test method then
static void test( ) throws NullPointerException{
int x=30,y=0;
int z=x/y;
}
message3
message4
run time error
If the test( ) method like this ,then it have an ArithmeticException ,but in the
catch(Exception e) {
System.out.println("message3");
throw e;
}
block , the ArithmeticException have been caugth.
why a RuntimeException will be thrown?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erico,
First of all, realize that an ArithmeticException "is a" RuntimeException, which "is a" Exception. This is inheritence.
Anyway, let's get back to your initial question before we get too far off track. The original code you posted was this:

And you claim that the answer states that the output should be:
message2,message4,
right?
However, if you run this code, you'll find that your output is:
message1,message4,message5
That's because nowhere in that code is an exception actually thrown. You have two methods that state that they can, potentially, throw exceptions, but neither of them actually do.
Look back at that question and see if you haven't misread or mistyped something because I can't see a way in which the execution path could ever print out message2,message4 by itself.
Verify that first and, once that's done, let us know what we can help you with.
Corey
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic