Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Propagation and ReThrowing Exception  RSS feed

 
Rahulkk Kumar
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception Propagation



ReThrowing 1



ReThrowing 2


All above these Three pack of codes producing same output
A: 50
Handled in Main Method

I want to know what is the difference between these three.
In which circumstance we used use Exception Propagation and Why ?
In which circumstance we used use ReThrowing1 and Why ?
In which circumstance we used use ReThrowing2 and Why ?
 
Rahulkk Kumar
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry there is typo 'used' must be 'should'
ex: - In which circumstance we should use Exception Propagation and Why ?
I apology
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul,welcome to ranch!

actually Exception is about or a kind of exceptional situation that can arise during run-time.now their are 2 things possible either you can handle it in your own code or simply pass it to client,who actually utilize your code,means you are ensuring the user that this kind of exceptional situation may happen during implementing original code so please make your own strategy to handle this.

now their are two types of exception-unchecked and checked.
1.checked-compiler checks for either you have done one of the things,above mentioned,or not.if not compiler complains.
2.unchecked(RuntimeException)-compiler don't care either you have done one of the 2 things or not.because these are unpredictable and one cannot handle any situation,he is not aware of.but if you are aware of,you can handle that one specifically if possible.

in your case you have used unchecked exception.
-->in your first case you have thrown the exception to the client(main) who handles it.
-->in second case you done something like this suppose their are 3 people,first has throw the ball to 2nd one and 2nd one has throw the ball to third one.why don't you directly throw it to 3rd one,if you just want to play with him (by using throws keyword in method declaration.)
-->in the third case you have done the worst thing.i.e.,deviate the user from actual cause of error.you have thrown the exception but change the type.please don't do this kind of thing.
in all the cases you will not be able to get the error source,because by handling them(as you does) you prohibited the propogation of exception on stack.if you are really handling them then try to do something from where your program can recover the exceptional situation(shouldn't crash).
if you really don't know about how and in which circumstances your code is going to be implemented then simply throw it to user he knows well about situation.
second thing if you are handling it in your code make it sure then it is not going to cause any unfavourable conditions in user code.so better will be to simply throw it to user,but sometimes it is not possible to throw the exception like in run() method of Runnable interface,so their you have to handle it.

Hope this will help!

kind regards,
Praveen.
 
Rahulkk Kumar
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I absorbed :-
ReThrowing 2 -
We can convert one type of exception into another for this we have to create exception object and throw it manually to corresponding catch block.

Rethrowing 1 -
Just becoz we are not creating exception object so corresponding catch block must contain same type of exception hence In this example we can't convert one type of exception into another but we can handle same type of exception in corresponding catch block
 
Rahulkk Kumar
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be an advantage for example if user click any wrong link (any type of exception) they will land to one specific page (one specific catch for handling all type of exceptions).
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahulkk Kumar wrote:What I absorbed :-
ReThrowing 2 -
We can convert one type of exception into another for this we have to create exception object and throw it manually to corresponding catch block.

it seem incorrect to me,i don't get,what actually you mean by converting an "exception".but suppose if method is declared to throw an exception "e" and you
are overriding method in subclass you can throw its subtype.
one more thing suppose you are handling the 2 Exceptions "e1" and "e2" and e2 is subtype of e1,then you must have to handle e1 first,because handling e2 first makes handling of e1 unreachable as e1 is a e2 by inheritance.

Rahulkk Kumar wrote:Rethrowing 1 -
Just becoz we are not creating exception object so corresponding catch block must contain same type of exception hence In this example we can't convert one type of exception into another but we can handle same type of exception in corresponding catch block.

no it's not necessary the catch block can contain its supertype.like in your case  user can handle the RuntimeException(nonsensical) rather than Arithematic Exception.
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't "do" exception propagation.  This term refers to what happens to exceptions if you don't handle them: they get propagated up the call stack until they are handled or reach the end of the call stack unhandled, at which point the runtime system will terminate.

None of the examples you gave are very good. First, both ArithmeticException and NullPointerException are RuntimeExceptions. Since these are caused by programming errors, they should be allowed to propagate and terminate the program. Then you should go fix the code so that it doesn't cause runtime exceptions. Second, your examples don't include a context that show the conditions under which you would decide to do one of three things: 1) simply declare the exception and allow it to propagate, 2) catch and rethrow, or 3) catch a low-level exception, wrap it a higher-level exception that is thrown up the call the stack.

Please read through the Exceptions track of the Java Tutorials.

Other good resources are:

- Effective Java by Joshua Bloch, some of which is discussed here: https://www.ibm.com/developerworks/library/j-jtp05254/

- Robust Java by Stephen Stelting


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!