• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Why can't we use "throws Exception" for a class ?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can't we use the "throws Exception" for a class ?

Like "class Demo throws Exception" ???

Hope it is not too silly !
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you did that, what should it mean?
 
Aman Kulkarni
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:If you did that, what should it mean?




I haven't tried it yet due to some java unavailability...I am for holidays and the only thing I have is the internet connection...So thought of asking it !
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aman Kulkarni wrote:

Stephan van Hulst wrote:If you did that, what should it mean?




I haven't tried it yet due to some java unavailability...I am for holidays and the only thing I have is the internet connection...So thought of asking it !




You don't need availability to java to answer the question. Let's assume that it was allowed, what would you expect it to do... After all, you asked why we can't use a feature? You should have an idea of what this feature will do right?

Henry
 
Aman Kulkarni
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Aman Kulkarni wrote:

Stephan van Hulst wrote:If you did that, what should it mean?




I haven't tried it yet due to some java unavailability...I am for holidays and the only thing I have is the internet connection...So thought of asking it !




You don't need availability to java to answer the question. Let's assume that it was allowed, what would you expect it to do... After all, you asked why we can't use a feature? You should have an idea of what this feature will do right?

Henry



If we use it in the class itself...All the exceptions of the entire class will be handled....I am asking is it allowed ??
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That seems non-intuitive. Throwing and handling exceptions are polar opposites. When you use the throws keyword on a method signature, it doesn't mean the method handles the exception. It means it may throw that exception under certain circumstances, and code calling the method should deal with the possibility that that particular exception may be thrown.

It doesn't make sense on the class level. So no, you can't do this.
 
Aman Kulkarni
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That seems non-intuitive. Throwing and handling exceptions are polar opposites. When you use the throws keyword on a method signature, it doesn't mean the method handles the exception. It means it may throw that exception under certain circumstances, and code calling the method should deal with the possibility that that particular exception may be thrown.

It doesn't make sense on the class level. So no, you can't do this.




Ok ! thanks a lot !!! You see I am still kinda newbie...So please bear with some stupid questions
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem. Keep practicing!
 
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No question is silly question.

In Object Oriented Paradigm Class provides template and object act as implementation to these templates in the form of methods , instance variables.
Java provides "throws Exception" at Object level i.e in constructor as well as in methods.
So it makes sense to have throws clause at method level rather than class level.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aman Kulkarni wrote:

Henry Wong wrote:

Aman Kulkarni wrote:

Stephan van Hulst wrote:If you did that, what should it mean?




I haven't tried it yet due to some java unavailability...I am for holidays and the only thing I have is the internet connection...So thought of asking it !




You don't need availability to java to answer the question. Let's assume that it was allowed, what would you expect it to do... After all, you asked why we can't use a feature? You should have an idea of what this feature will do right?

Henry



If we use it in the class itself...All the exceptions of the entire class will be handled.



That doesn't make any sense. Declaring a method throws Exception doesn't handle any exceptions, so why would you expect it to do so on the class?

...I am asking is it allowed ??



Write some code and see for yourself.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aman Kulkarni wrote:

Stephan van Hulst wrote:If you did that, what should it mean?




I haven't tried it yet due to some java unavailability...I am for holidays and the only thing I have is the internet connection!



Then you have access to a Java compiler.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chandraprakash Sarathe wrote:No question is silly question.



Oh, there are definitely some very silly questions.

In Object Oriented Paradigm Class provides template and object act as implementation to these templates in the form of methods , instance variables.



Which has nothing to do with the question asked.

Java provides "throws Exception" at Object level i.e in constructor as well as in methods.



Constructors throwing exceptions is not "at object level".

So it makes sense to have throws clause at method level rather than class level.



That doesn't address the question at all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic