• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Exception handling  RSS feed

 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have read that ,,if a method throwing an exception we have to handle it by using try /catch block ,or we have to declare that method with Throws clause,
But in the 2nd situation also we have handle it , but in some method which is calling it, but i am ducking exception until main method ,

for ex: Class One {

methodOne() throws BAdException{
-------
----
throw new BAdException();
}

Class Two{

public static void main(String args[]) throws BadException {
One o=new One();
o.methodOne();
}

}
in above example in The Class Two iam calling methodOne which throws An exception but i am not handling , instead i am Throwing out of main() . can i do like that

what will happen in this scenario if we throw out of main method....
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you try that code? what do you think would be the result? imagine main method receiving the exception but not handling it .
and yes it is possible for a main method to throw an exception.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rajendar talatam wrote:i have read that ,,if a method throwing an exception we have to handle it by using try /catch block ,or we have to declare that method with Throws clause,
But in the 2nd situation also we have handle it...

What makes you think that? What do you think Exceptions are for?

My suggestion: read the Java tutorials.

Winston
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
rajendar talatam wrote:i have read that ,,if a method throwing an exception we have to handle it by using try /catch block ,or we have to declare that method with Throws clause,
But in the 2nd situation also we have handle it...

What makes you think that? What do you think Exceptions are for?

My suggestion: read the Java tutorials.

Winston

means i have read that if i throw (using Throws) Exception also , somewhere you have handle it right,

my doubt is we are not handling it in main() also right....
just throwing out of main(),
i.e to the caller of main();
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Handling exceptions in main method is no different to any other method.

Basic rule: unless you know how to handle an exception (catch), you should always throw it (throws).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!