• Post Reply Bookmark Topic Watch Topic
  • New Topic

can Exceptions be returned  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it is said that an Exception is thrown then can it be interpreted in a way that the method RETURNS an object of type Exception?
If so then suppose the method has a return type of integer and has if else statements. If the if block is executed then an Exception is thrown otherwise some integer say 1 is returned, but then the type of Exception object is not an integer.
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

Returning an object reference means the method has reached the end of its code and completed its activity; throwing an Exception means that control of flow is taken away from it.Unless you catch whichever exception it is in method1, method1 doesn't complete its actions because exceptions take control flow away from their methods. So throwing an Exception and returning one is different. You cannot write this sort of thing and get the Exception printed:-No, in that case the exception is not returned. It propagates via a different mechanism. If the number is < 5%, you will not have it printed out. The System.out.println call will never be executed, and it will not print the exception details.

There are a few methods which return exceptions, e.g. this one, but calling that method does not cause the exception to be thrown. Returning an exception allows the program to follow its normal control flow and that does not mean the exception is thrown. (Usually there is no exception, in which case that method will return null.)Because I didn't write throw, the catch block will never be executed; indeed it is likely that the compiler will complain that the try doesn't actually throw that Exception.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saishradha Mohanty wrote:suppose the method has a return type of integer and has if else statements. If the if block is executed then an Exception is thrown otherwise some integer say 1 is returned, but then the type of Exception object is not an integer.

This is the first time I have ever seen a question like this and I can't imagine what your motivation is for asking it. Java is a strictly typed language so if you declare a method to return an Integer, there's absolutely no way you can write code in it that returns a value of any other type. As Campbell already mentioned, exceptions are propagated via an entirely different mechanism.

I'm still curious though, what motivated you to ask this question? What were you thinking of doing and why?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:what motivated you to ask this question? What were you thinking of doing and why?

Just to clarify my reasons for asking you this, people often ask questions that pertain to implementation details without being clear about their ulterior motives.

For example, someone might ask "How do I replace characters in a String?" and someone might answer "You can't, because String objects are immutable." While that answer is technically correct, it doesn't address the OP's ulterior motive which may have been something like taking a template such as "The ${animal} lives in a ${home}" and getting "The bird lives in a nest" or "The monkey lives in a tree".

So, again, my curiosity in why you asked the question goes back to wanting to know what you really want to do. Perhaps there is a different way to think about your problem and its solution other than returning an exception instead of the declared type, which we've already established is not possible.
 
Saishradha Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, if an Exception is thrown thrown then the program should terminate at that point of time, according to that then


This program should terminate at the point the exception is thrown and then in my output the last line of exception class should not be printed, but in my output, I am getting
Aaargh!
The event is over
no,
this no should not be printed then!
 
Saishradha Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, why it prints zero also, because the exception is caught!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saishradha Mohanty wrote:So, if an Exception is thrown thrown then the program should terminate at that point of time. . . .
Not quite. The current statement being executed is wound back, the control passes to the first catch which can handle that particular exception. If there is no catch, control passes to the calling method, and the procedure starts again.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!