What Is The Difference Between Throw And Rethrow
throw :-
“Throw” is an activity used for exception handling within a workflow. It allows you to explicitly raise an exception or error, which can then be caught and handled by the appropriate exception-handling mechanism. The “Throw” activity is part of the Control category in the UiPath Studio activity palette.
EX :-
Spet 1 :-
Here we will see an example . we take an excel input sheet like given below
Step 2 :-
- Next we go with studio, open the studio and add a new sequence drag and drop the read range workbook activity fill the requirements. The output is dt_table
- And then take for each row in datatable, give the data table dt_table
- The If activity is to be dropped in for each row in datatable and give condition “CInt( CurrentRow(0))<100“.(The amount should be less than 100)
- In the “Then” process take the Log message and give the ” the amount is less than100″ as message .
- In the “Else” part take another log message and give message” the amount is greater than 100″.
- Out side of the loop give a log message with message of “process completed”
- When we run the bot it will show output as given bolew
Step 3 :-
- Here our real process starts
- Drag and drop the “Throw” activity in “Then” process and give a exception called “New BusinessRule Exception” and message(“the avilable amount is lessthan hundread”)
- when we run the process it stops the process it shows message as “Uipath.core.BusinessRuleExeption”. It did’t go into process completed state.
Step 4 :-
- For hadling this exception the entair squence surround with try catch.
- In catch block we have to give Business RuleExceotion exception and take a message box give a message as ” exception.messsage” .
- it will throw the exception given like this
- The try catch block has one section called “Finally”. in Finally block we have to give a Log message with message of “Process completd”.
- When we go final process.
- When we got exception it thrown the exception and went into process completed state In the above image you can see exception handled and process copmpleted.
Moreover, the Throw activity promotes better collaboration and communication among team members. Exception handling becomes more transparent and accessible, as developers can clearly define the exceptions that may occur and the corresponding actions to be taken. This facilitates effective collaboration, as team members can understand and address exceptions more efficiently, reducing development time and enhancing productivity.
rethrow :-
Rethrowing an exception generally refers to the act of catching an exception and then throwing it again to allow it to propagate up the call stack for further handling. This concept is more commonly used in general programming and exception handling, rather than being specific to UiPath.
EX :-
Step 1 :-
- In the same process we would check “Rethrow” also
- We comment out the “Throw activity” drag and drop the “Rethrow activity” in catch block .
- For our understanding porpose name this first try catch as children try catch
- Give another try catch it clled as parent try catch.
- Drag and drop the “Rethrow” activity in children try catch
- And give message box as “exception.message.
Step 2 :-
- In the above processthe we saw the “Throw activity” thrown the exception and stops the process, but Rethrow would not stops the process it will countinue the process
- In parent catch block also we have to give Business RuleExceotion exception.
- One message box also has to be given. In message box give the message”i am in parent try catch”.
- And take another message box give a mesaage like Exception.message+”-“+exceptiop.source.
Step 3 :-
- Finally when we run the bot we can see how Rethrow works.
- And go to next step we can see
- Here the balace is avilable less than hundread, and we entered in parent try catch it will show
- It entered in parent try catch now Rethrow will work.
- Here we can see how rethrow will work
Additionally, the Rethrow activity promotes better collaboration and communication among team members. When exceptions occur, the Rethrow activity allows developers to pass the exception information along with contextual details to other team members or downstream processes, facilitating effective problem-solving and reducing development time.
NOTE :- Rethrow can be used in only in catch blog
here you can see in table formate :-
Throw Activity | Rethrow Activity | |
---|---|---|
Definition | An activity that generates and throws an exception | An activity that rethrows a previously caught exception |
Usage | Typically used within a Try-Catch block | Used within a Catch block to rethrow an exception that was caught |
Syntax | Throw New ExceptionType("message") | Rethrow |
Effect | Raises a new exception for handling | Continues propagating the caught exception |
Exception | Creates a new exception object | Reuses the caught exception object |
Handling | Requires explicit Catch blocks to handle | Requires an enclosing Catch block to handle |
Multiple | Can be thrown from different code locations | Cannot be used outside a Catch block |
Stack Trace | Creates a new entry in the stack trace | Retains the original stack trace of the caught exception |