How to use Throw and ReThrow

do you have any sample example of xaml?

1 Like

Thank you all … I now am using throw and rethrow effecftively.

Here is the key missing information that I needed and that uipath fails to document:

when an exception occurs uipath creates an “exception” object named: exception.

The exception object has many attributes such Message - which is the actual message text.

rethrow takes an existing exception and regenerates it - so that it can be caught by a higher level try-catch - or not - meaning it will stop the robot and display the exception.

Throw is for generating your own exception - and the KEY piece of missing information is that you must first instantiate a new object of type object and populate the Message attribute or other attributes.

Can somebody - anybody please explain to me why this is not Documented by uipath??? Or if it is - please send me a reference :

13 Likes

Most of what you’re asking here is covered in the academy lesson 12 (I don’t think Throw is though). At least to a level where you should be able to understand how to use it in most cases, not necessarily how it works.

There’s IMHO 2 reasons why for these parts UiPath doesn’t go too deep, which are themselves a Catch-22:

  1. It’s already documented for .Net and Workflow Foundation by Microsoft, so it’s redundant. But if you’re not a programmer, you might have a hard time reading into it or even finding it.
  2. If they do document it, it’s redundant and either the documentation will be worthless for the most part (don’t go deep) or it will repeat what’s already on MSDN.

There’s no easy solution to that, as the audience for UiPath is very broad - from very technical to business people. You can’t satisfy all.

Some small corrections though:

Exception is created by the code that was trying to execute and failed. The “exception” object in the Catch clause is an object where exception data is stored once it bubbles up to that point. Btw - you can change the name as you please.

Not exactly. Rethrow let’s the exception pass further up - it’s still exactly the same exception (this is a key difference between using Rethrow and Throw(oldException) ).

You probably know that, but for potential other readers - object created needs to be of type Exception or any of the derived types (ArgumentException, BusinessRuleException).

And that’s the thing - you can dive as deep as you want with this, question is how for is it actually worth it, since it’s already there by other parties (MSDN and programmer sites). It’s an unfortunate reality for non-programmers - sooner or later you will need to start using programming knowledge (you already are if you’re using UiPath, even if you don’t realise it) or you will hit a usefulness ceiling.
:frowning:

20 Likes

Indeed, all programmers do in the end is come up with algorithms to automate tasks or solve problems. UiPath just allows you to “program” at the level of application interfaces, almost without the direct use of the underlying programming language. But in the end, your workflows are compiled and executed just like hand-written code.

As Andrzej says, it is hard to provide the right level of training for every individual learning UiPath. However, I do think there could be value in some extra courses about the basic programming concepts that relate to UiPath; topics like objects, instantiation, methods, operators. The definitive documentation is of course MSDN, but I feel it would help a great deal to give people some background about what the tutorials make them type. Like: myString.Trim, myString.Replace(...) are invocations of methods of the String class on the instance myString; Message is a String property of the Exception class. This is easy to read if you understand the jargon and rather a mouthful of buzzwords if you don’t. Having some understanding will allow you to search and interpret results more effectively.

My own history is that I learned programming, though not in VB or other .NET languages, before UiPath. It is my experience that I can often answer questions fielded here by applying just my conceptual knowledge to find the right documentation and interpret its technical terms and examples. While this often leaves people with a solution, the questions are frequently very specific: a user wants some steps to solve a particular problem in the workflow he/she is developing. This is of course fine, but sometimes the best solution is an improved insight into the programming concepts behind it, which you don’t always get this way.

Therefore I compliment you for asking about this very important concept in programming. :grinning: What you have just discovered is also why it is difficult to document some things in full detail: instantiating objects is the core business in object-oriented languages and it permeates everywhere. Exceptions are just objects like any other; nothing special happens until one appears in a Throw statement.

16 Likes

Hi all,

Can we Execute UiPath Process by using application program interface?
Thanks.

1 Like

Only if you have your Robots connected to Orchestrator.
https://orchestrator.uipath.com/v2018.1/reference
https://platform.uipath.com/swagger/ui/index#/

2 Likes

Thank you, Ovi.

1 Like

Please find below attachment,
Main.xaml (10.5 KB)

Cheers,
Pankaj

3 Likes

@ClaytonM please post a workflow to explain your post.

2 Likes

Hi @danesh,

Check out the post right above here:

The Exception you use in the Throw depends on what you are looking to throw. In his example, he is using a FileNotFound exception, but you can use others. And, if needed, you can also place a Rethrow in the Catch and it will throw the exception that had occurred. (to be honest, I use Rethrows temporarily more for troubleshooting than actually a part of my code; it’s a good way to pause and figure out what errors are happening, although you can also use a Message Box with the below information and is just as good)
exception.Source+"__"+exception.Message

6 Likes

The functionality of Throw is clarified. Could anyone please elaborate a scenario differentiating where Rethrow can be used and how it differs from Throw. The statement “activities are occurred before exception” is a bit confusing. what is it exactly meant by. Can anyone share the knowledge explaining through examples how throw and rethrow are differentiated??

4 Likes

Hi,
I´m not the most technical person and I might say something wrong, so please correct whoever knows, but from what I´ve seen…

Let´s say you´ve got a try/catch activity where the try throws an exception. If you put a Rethrow activity in the Catch block and run the robot from UiPath Studio, then you will get the pop-up window with the exception message and details, and the robot will stop.

On the other hand, if you run that same process from Orchestrator, I think that the robot will give you the error details, but it will continue running.

Please, correct me if I am wrong.

2 Likes

Hi @ander.labaka

Welcome back!

No, the idea of throwing an exception is to either stop the process or handle that particular exception. If you re-throw an exception, it will stop the process no matter if it was run from Studio during development or from Orchestrator.

8 Likes

I have implemented an example for the same and what i have observed is…

  1. If i put throw in my Try block it will just throw whatever customized exception I have included in my throw. This will just through a pop up displaying the exception message.
  2. If the same has been handled used Rethrow in Catch block, an exception is thrown instead of a message box and the process is stopped abruptly. This exception is like any other exception containing the information mentioned in Throw(Source, Message, Exception)
3 Likes

This is correct. If you have an exception and you catch it, it will not stop your process. However, When you rethrow in the Catch block, then it will need to be caught in a Try-Catch above or else it will throw an exception an stop the process.

8 Likes

Thanks.

new Exception(“Throwing an exception because something went wrong”)

4 Likes

Hello Guys!

I tried using the throw and rethrow but I’m always getting the throw message in the catch even if there’s no error in the code. Would you be able to help on how to use rethrow and throw properly? I’m using throw and rethrow because I want to customize the error message that I’m having. Thanks

Cheers

3 Likes

What I did was

Try catch

throw (new exception (“msg”)
Steps
Catch
System.exception (exception.message"

3 Likes

@bdavidov - In simple terms Rethrow is used when you want activities to occur before the exception is thrown. Throw activity is used when you want to throw error before the execution of the step

3 Likes

Hello @sandipauti

How are you? Would you be able to help me to share your knowledge on how to use throw and rethrow or use customize exception? if you have a simple sample, please share. thanks

3 Likes