• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Urgent

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Unchecked1{
public static void main(String[]args){
}
void method(){
try{
metod1();
System.out.println("Testar metod1");
}
catch (ArithmeticException ae) {
System.out.println("F�ngar ArithmeticException");
}
finally{
System.out.println("K�r finally");
}
System.out.println("I method() utanf�r finally");
}
void metod1(){
throw new NullPointerException();
}
}
A. K�r finally. I method() utanf�r finally. F�ngar Exception.
B. K�r finally. F�ngar Exception.
C. None.
D. K�r finally.
ans is c
i complied & executed the program..the result was None..
first i thought the ans is D.(finally will always execute)..
can anybody pls explain
thanx
sherin
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sherin,
I compiled the prog & I got the following o/p.

Kor finally
Exception in thread "main" java.lang.NullPointerException
at Unchecked1.metod1(Unchecked1.java:36)
at Unchecked1.method(Unchecked1.java:25)
at Unchecked1.main(Unchecked1.java:20)
The finally will always be executed, except exiting the JVM.
Aruna
[This message has been edited by Aru (edited September 20, 2000).]
 
sheri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi aruna
i again complied & run the pgm..but the result is the same..none

how did you get the o/p
thanx
sherin
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sheri!
Your main method is empty, it doesn't call method().
Put smth like this:
class Unchecked1{
public static void main(String[]args){
new Unchecked1().method();
}
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Notice close, main doesn't call any methods or create any objects. This is why you get nothing output.
If main had the lines

Then the answer would be D.
The exception is throw so no code after the line causing the exception and before the catch is executed. It isn't caught so no code in a catch or outside the finally (before or after) is execute. The finally always gets executed (except in System,exit(0)) This contians the only output.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sherin,
The answer c is correct 'cause the main is empty! There is no call to method() so, there is no try, no catch and no finally
Hope that cleared your doubt.
Shubhangi.
Originally posted by sheri:
class Unchecked1{
[B}
public static void main(String[]args){
}
[/B]
void method(){
try{
metod1();
System.out.println("Testar metod1");
}
catch (ArithmeticException ae) {
System.out.println("F�ngar ArithmeticException");
}
finally{
System.out.println("K�r finally");
}
System.out.println("I method() utanf�r finally");
}
void metod1(){
throw new NullPointerException();
}
}
 
sheri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx to all of you..
 
pie. 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