
UiPath Exception Handling Example
UiPath Exception Handling Example
Here’s an example of how to do Exception handling in UiPath. In this example, we take our Number Guesser application and put the whole thing in a global UIPath Exception handler so that any errors or exceptions are caught. We examine the whole UiPath try catch finally block structure and make our application a little bit more bulletproof than before.
Content
0.4 -> hey cameron mckenzie here at cameronmcnz on
twitter and i wanted to talk to you about
4.64 -> how you do some exception handling in uipath
namely how you use the try catch finally block
11.04 -> send some messages to the user maybe if an error
happens and also how to do a little bit of logging
16.88 -> if you want to send a message into your log files
when an exception happens in my previous example i
22.32 -> built this number guesser application it only took
about five minutes to build it in that tutorial so
28.16 -> check that out if you want to start this from
scratch but as you can see basically it asks me
32.56 -> to pick a number and if i get the number right
it says hey you picked the number right but if
36.8 -> i play this and i don't provide any input at all
all of a sudden i get an exception that says hey
44.32 -> system not supported error something really really
went wrong so one way to avoid that is to put try
51.04 -> catch blocks into your applications so what i'm
going to do here is i'm going to go into system
55.92 -> i believe it's under statements and there you
can find the try catch block element there and
62 -> i'm actually going to put it right to the top so
i'm going to surround everything in a try catch
66.32 -> block and so there's my try catch block so try
to do something catch any exceptions and then
71.44 -> finally close everything up and what i'm going
to do is i'm going to take my whole activity
77.76 -> and i'm going to drag that whole activity right
into the try block there and i'm also going to
82.16 -> drag that message in as well
89.2 -> and there we go i'm going to take a better look at
that maybe go to a hundred percent but you can see
96.08 -> my sequence now has this tri-catch block that
surrounds everything everything happens within
101.2 -> this try catch block and if there's an error i
can catch it and i can exit the program eloquently
106.8 -> now one thing i should say user input shouldn't
throw an exception so really i probably should be
111.52 -> doing user input validation that's a better best
practice but it's a good way to just demonstrate
115.84 -> how exceptions work and so what you do is you
do all of your work and then you try and catch
120.8 -> an exception in this case i've got the system
exception that i need to worry about notice you
126.08 -> can catch multiple ones just generic exceptions
system exception null reference exception i'm
131.52 -> going to catch the system exception and when you
catch it you've got to do something and so what
136.08 -> am i going to do well i guess i could always
throw up a message box and say hey you didn't
145.76 -> supply a number just kind of get mad at the
person and you can have multiple catches so
151.76 -> you can add a new catch block here maybe
you just wanted to catch the most generic
155.52 -> system exception here and i don't know what
you want to do here you can always you know
159.92 -> one thing people like to do is they like to log
so you can also throw a log message in there and
164.96 -> set the log level to warn and hey hey we need
input validation and that's really the solution
172.8 -> to this problem the solution this problem is
not throwing an exception it's dealing with
178.08 -> input validation so exceptions aren't thrown but
again we're just kind of demonstrating thing here
182.8 -> and then there's your finally block and
the finally block always runs and so no
186.8 -> matter what happens the the finally block will
run and i'm going to just say here and just
192.88 -> say
200.4 -> and as i said that will actually run regardless
of whether an exception is thrown or not so
205.52 -> that's that's your idea of the finally block
and so there you go now we've got a nice tight
210.64 -> application where we put everything in a try catch
block hopefully the number run the application
215.76 -> runs as expected but if there is an exception
we will get an error message and a message box
222.88 -> there so let me try and run this application now
as i said when i just type numbers in normally
230.72 -> everything seems to look good but if i don't
type anything in notice it now says hey you
236.4 -> didn't supply a number that is the result of the
catch block being triggered hey you didn't supply
242.16 -> a number and then when we're done on here
it's going to go down to this finally block
249.12 -> and it now it says we enjoyed having you around
and now one thing you'll notice that even if there
253.68 -> isn't an exception so if i play this and i type
in the number five that means that i'll win so you
260 -> pick the right number and the finally block runs
again no matter what so that finally block always
265.76 -> runs whether there's a exception or not one of the
things you often do in that is closed resources
270.96 -> you've got a database connection or a connection
to a file and you want to make sure that that
275.52 -> connection is closed regardless of whether there's
an exception or not usually you code that inside
279.92 -> of the finally you don't really make the finally
part of your application flow control but we did
285.52 -> there and it worked there you go that's how you
do basic exception handling in ui path now just
292.4 -> want to let you know this is kind of the the
second in a multi-part set of tutorials so i'm
297.6 -> going to be taking this number guesser and adding
conditional statements and loops and enhancing it
302.8 -> in a variety of different ways so follow on with
the other tutorials if you really want to learn
307.44 -> how to do ui path programming properly also if
you're interested in more enterprise software
312.32 -> development tutorials and articles head over
to theserverside.com i'm the editor-in-chief
316.8 -> over there you also follow my personal antics on
twitter at cameronmcnz and subscribe on youtube
Source: https://www.youtube.com/watch?v=Yy-IEHLIjzo