• 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:

Can amethod can return an Exception

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it true:
Can amethod can return an Exception?

My answer isNo, i cannot. Please correct me if I'm wrong.


pls help
regards,
gitesh
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gitesh Ramchandani:
Can amethod can return an Exception?

My answer isNo, i cannot. Please correct me if I'm wrong.



Why not? Try writing a function returning an exception.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possibe. Method can retutn a exception also.

Ex:

Exception demo()
{

return new IOExcetion();
}
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Al Mamun

You said we can return an Exception i agree to this
but i have some doubt on this as below

If any method returns Exception than the calling method
has to call the method in try catch block?
bcoz the method is returning
Exception......
just as shown below





Please help
i need some explanation on this

Thanks in advance!!!
[ August 28, 2007: Message edited by: dhwani mathur ]
 
Abdullah Mamun
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dhawni

Originally posted by dhwani mathur:
If any method returns Exception than the calling method
has to call the method in try catch block?
bcoz the method is returning
Exception......



The calling method need not to be in the try block. This idea is same as any other functions. Have a look at the following example:



This will work just fine and produce the output Returning Exception!!!
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dhwani, a try-catch block will be required if the method were throwing an exception. In this case you are simply returning an exception just as you would any other object.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey !!Thanks a lot

Al Mamun and Saket Barve

Now i am clear with my doubt!!!

Preparing SCJP 1.5
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody,

My doubt is cleared now.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The one line answer would be: Yes you can return exceptions since they descend from Object class and are a type of Object.
Fellow ranchers, avoid long-winded answers. Rather be brief and to the point.
That helps answering the question as it is,rather than spawning multiple threads of useless breath.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic