Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pulbic static void main(String args[])throws Exception

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

What if main method itself declares that it throws the exception , it is not handling it at all , STILL PROGRAM IS GETTING COMPILED AND RUN ...

who is handling in this case the Exception of main ?


Please anyone put some light on it.

Source : http://www.javabeat.net/javabeat/scjp2/mocks/scjp_1_4_mock_exam_questions_3.php


Q6 public class Test6{
public static void main(String args[]) throws Exception{
try{
throw new Exception();
}
finally{
System.out.println("No Error");
}
}
}
What will be the output?
A1 No Error followed by java.lang.Exception
A2 java.lang.Exception followed by No Error
A3 No Error
A4 Compiler Error



Thanks
Vishal
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I came across very good link :Just for information of you all
Various acceptable forms of main method (very nice)
http://rationalpi.wordpress.com/2007/01/29/main-methods-in-java/

But still my doubt hasnt been expalined there
 
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,
I think A1 is correct one.
because you declared main() as throw Exception,when JVM calls main(),Exception
is thrown by main,whicn cant handled by JVM,so which stops execution of program,but finally executed at any condition,so output is
No Error,followed by java.land Exception
you didnt get any error at RunTime.
Regards
Venkat
 
Venkat Kanneganti
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,
sorry,replace RunTime with CompileTime
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
If you throw an explicit exception I mean like this "throw new Exception()" it needs to be catched somewhere in the call stack atleast at the end of stack in the main method.If you won't catch it atleast at end of call stack you will end up with corresponding exception at the runtime.And one more thing "throw new Exception" and "throws Exception" both are different.In the example which you have shown you are not catching the exception your are just escaping from the compiler by declaring the exception in the main method.
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Navin & Venkat,

Thanks a lot guys for your respones!!!

Yes,Naveen
There are two situations :
1)If our main method declares as well as throws the exception : In this case it gets compiled but throws exception as venkat correctly said for my above pasted question ,here the answer is A only.

2)If our main method only declares the exception but doesn't throw it , then it gets compiled as well as run , i tried doing it as below :


public class Test6{
public static void main(String args[]) throws Exception{
}
}

i also checked on sun blogs , this (2) is useful for debugging purpose , i didnt get howcome.
:roll:

Now i think i have digested this concept a bit
Thanks guys!!!

Vishal Chugh
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic