1
00:00:05,140 --> 00:00:09,130
Alright, so let's talk now about
handling invalid arguments. What I'm

2
00:00:09,130 --> 00:00:13,320
going to start off by doing, is deleting
the last few lines from the previous video,

3
00:00:13,320 --> 00:00:15,800
because I don't want to leave you
with code that you shouldn't write.

4
00:00:15,800 --> 00:00:19,020
So I'm going to go ahead and delete that code

5
00:00:19,020 --> 00:00:25,260
Alright, so looking back up, or scrolling back up
to line three now, in our banner_ text function.

6
00:00:25,260 --> 00:00:29,660
We're checking that the string we want to
print will fit in a specified width.

7
00:00:29,660 --> 00:00:33,520
We haven't tested that part of our function
yet, so we'd better do that now. Rather

8
00:00:33,520 --> 00:00:36,850
than messing about with all our strings
in the main code, what I'll do is just

9
00:00:36,850 --> 00:00:42,860
reduce the width on line two. We're gonna
set that to 50 instead of 80.

10
00:00:42,860 --> 00:00:46,880
Alright, so now that I've done that, I'll run the program again.

11
00:00:47,620 --> 00:00:54,380
And you can see, looking at that output,
it's absolutely horrible - I'm sure you'd agree.

12
00:00:54,380 --> 00:00:58,840
A few of the lines are too big to
fit in 50 characters. So what can we do

13
00:00:58,840 --> 00:01:03,309
in situations like this. If you recall, I
chose to print a warning message and you

14
00:01:03,309 --> 00:01:07,930
can see that there in the output. It's
obvious from the output that something's

15
00:01:07,930 --> 00:01:11,229
gone wrong, and the message explains what
happened; the text is too long to fit

16
00:01:11,229 --> 00:01:16,220
in the specified width. That's not a bad
approach here but it does have drawbacks.

17
00:01:16,220 --> 00:01:20,100
If we were setting this output to a
machine that automatically prints

18
00:01:20,100 --> 00:01:24,440
plastic signs, then the result wouldn't be
very good. The best we could hope for

19
00:01:24,440 --> 00:01:28,760
is that one of the plastic sheets is
wasted. The worst that could happen

20
00:01:28,760 --> 00:01:33,000
is that the laser burns a hole in the
operators shoe, when our code sends it

21
00:01:33,009 --> 00:01:36,880
past the end of the sheet. Python's a good
language for controlling robots and

22
00:01:36,880 --> 00:01:41,380
machinery, but if your resume says Wrote a
program that burnt people's feet off, I'm

23
00:01:41,380 --> 00:01:45,100
sure you'd agree, it wouldn't look good.
So what I'm going to do instead, is cause

24
00:01:45,100 --> 00:01:51,360
the function to crash, and we can do that
by raising an exception.

25
00:01:51,860 --> 00:01:55,350
So what I'm going to do, is close down
the output pane,

26
00:01:55,350 --> 00:01:59,700
and I'll actually enter the text first -
the code - and then we'll talk about it

27
00:01:59,700 --> 00:02:04,140
once we've seen it working. So I'm gonna
start off by deleting these two lines here,

28
00:02:04,140 --> 00:02:09,039
lines four and five now. And instead,
I'll tap over there and replace that

29
00:02:09,039 --> 00:02:14,420
code with this; so I'm going to type
raise ValueError, so space and a Value

30
00:02:14,420 --> 00:02:18,580
Error and a capital V. You can see there
that IntelliJ is doing that for us.

31
00:02:18,580 --> 00:02:22,760
In parentheses and in double quotes I'm
going to type String, using a replacement

32
00:02:22,760 --> 00:02:33,360
field zero, so curly braces with a zero
in it, is larger than specified width.

33
00:02:33,360 --> 00:02:39,400
Another replacement field, the second one, one - so one
in curly braces - closing double quote.

34
00:02:39,400 --> 00:02:47,760
On the next line we'll do a dot format
parentheses text comma screen_width,

35
00:02:47,760 --> 00:02:51,540
and we've got our two closing
right parentheses there. Alright, so now

36
00:02:51,540 --> 00:02:55,480
that I've done that, if we run the program,

37
00:02:55,480 --> 00:02:59,120
remembering what we got last time. This
time when we run it, we've now got a crash,

38
00:02:59,120 --> 00:03:04,040
as you can see on the screen there. So a
program that crashes isn't ideal,

39
00:03:04,040 --> 00:03:07,819
but it's definitely preferable to burning
someone's foot off with a laser.

40
00:03:07,819 --> 00:03:11,970
Sometimes there's nothing else your
functions can do. At least we've made the

41
00:03:11,970 --> 00:03:16,020
programmer aware that they've made a
mistake when calling our function.

42
00:03:16,020 --> 00:03:19,620
Of course, most of the time that programmer
will be you, but you'll also write

43
00:03:19,620 --> 00:03:23,250
functions that other programmers will use.
It's quite common for different

44
00:03:23,250 --> 00:03:26,940
programmers to work on parts of a large
program. Alright, so have a look at the

45
00:03:26,940 --> 00:03:31,290
error message in the output. It's quite
descriptive, and tells us which string

46
00:03:31,290 --> 00:03:35,100
caused the problem and what the
specified width is: 50. We can scroll over

47
00:03:35,100 --> 00:03:39,170
and we can see the complete error there.

48
00:03:40,680 --> 00:03:45,160
Now that's generated from the format
string, on line four of our code here.

49
00:03:45,160 --> 00:03:49,040
You shouldn't raise exceptions very often
but when you do, make sure you include

50
00:03:49,049 --> 00:03:53,010
enough information to help the
programmer who gets the error. So before

51
00:03:53,010 --> 00:03:57,349
we look at lines four and five in more
detail, a quick word about the output.

52
00:03:57,349 --> 00:04:01,799
IntelliJ shares the output in error
consoles in a single window. That means

53
00:04:01,800 --> 00:04:05,720
that you might see the exception
text mixed up with the program's output.

54
00:04:05,720 --> 00:04:09,320
You won't get that effect when your
program's executed normally. It's just the

55
00:04:09,329 --> 00:04:13,349
way things work in some IDEs. So don't
worry if things are mixed up in there -

56
00:04:13,349 --> 00:04:16,350
that's completely normal.
Alright, so let's have a look at the code

57
00:04:16,350 --> 00:04:20,459
that's raised the exception. That's on
lines four and five on-screen now.

58
00:04:20,459 --> 00:04:24,150
I split the code over two lines to keep
the line links within the PEP 8

59
00:04:24,150 --> 00:04:28,260
guidelines. It also makes them show up
better on video. So we start with the

60
00:04:28,260 --> 00:04:33,060
keyword raise - that's how you raise an
exception in Python. Next we specify the

61
00:04:33,060 --> 00:04:37,600
type of exception that we want to raise.
ValueError is a good choice here,

62
00:04:37,600 --> 00:04:42,320
because the problem was caused by one of the values that were passed to our function.

63
00:04:42,320 --> 00:04:51,100
Now if we open a browser we can check 
out some of the available exceptions.

64
00:04:51,160 --> 00:04:57,600
Alright, so I'm gonna scroll down here until we get to
the concrete exceptions area, down here.

65
00:04:57,600 --> 00:05:01,460
In here, you'll find an alphabetical list
of exceptions that you might get when

66
00:05:01,460 --> 00:05:06,520
your code contains errors. You can also
raise any of these exceptions yourself.

67
00:05:06,520 --> 00:05:10,120
There's probably only two of these that
you'd normally use in your own function.

68
00:05:10,120 --> 00:05:15,260
The first is type error - I'll just scroll
down a little bit so we can see that one.

69
00:05:16,599 --> 00:05:20,349
OK, you can see it on screen there now.
That's raised when an operational

70
00:05:20,349 --> 00:05:25,689
function is applied to an object of
inappropriate type. You may want to raise

71
00:05:25,689 --> 00:05:30,360
that if your function excepts an integer,
but it gets passed a string, for example.

72
00:05:30,360 --> 00:05:33,980
And the one we used here, if we scroll down a
little bit further,

73
00:05:34,610 --> 00:05:39,169
was a ValueError. That's raised when an
operational function receives an

74
00:05:39,169 --> 00:05:43,819
argument that has the right type, but an
inappropriate value. And that's what

75
00:05:43,819 --> 00:05:46,879
happened in our function - we got
strings that were too long. So this

76
00:05:46,879 --> 00:05:51,740
exception was a good choice, in our case.
You might want to also consider using

77
00:05:51,740 --> 00:05:56,500
the next one - the ZeroDivisionError. If
your code checks that a divisor isn't 0,

78
00:05:56,500 --> 00:06:01,280
you raise this exception if it is. I said
you may want to use this one, not that

79
00:06:01,280 --> 00:06:05,330
you will want to, because your attempt to
divide by zero will probably raise it

80
00:06:05,330 --> 00:06:09,780
for you anyway. If you attempt to divide
by zero, you'll get a ZeroDivisionError,

81
00:06:09,780 --> 00:06:14,300
without having to explicitly raise it
yourself. So make sure you bookmark this

82
00:06:14,300 --> 00:06:18,680
page because it's useful, even if you're
not raising your own exceptions. If your

83
00:06:18,680 --> 00:06:22,430
code crashes, you can look up the
exception in here, and maybe get a bit more

84
00:06:22,430 --> 00:06:26,719
information about what caused the crash.
Generally, though, the text of the error

85
00:06:26,719 --> 00:06:30,259
that you'll receive in the exception,
will help you to work out what went

86
00:06:30,260 --> 00:06:35,980
wrong. Alright, so back to our code. Okay,
so our function now informs the caller

87
00:06:35,980 --> 00:06:39,860
if they try to use a string that's too
long. But maybe they want to print longer

88
00:06:39,860 --> 00:06:43,820
strings. They might have put the plastic
sheet into the machine in landscape,

89
00:06:43,820 --> 00:06:48,240
giving a lot more width to print on. Our
function shouldn't restrict them to only

90
00:06:48,259 --> 00:06:52,550
80 characters - or 50, as it is at the
moment. You should really let them pass

91
00:06:52,550 --> 00:06:56,870
in the width, as another argument to the
function. You may have noticed by now,

92
00:06:56,870 --> 00:07:02,349
that whenever I say something like you
should, it's time for another challenge.

93
00:07:02,510 --> 00:07:06,620
Now the challenge is to

94
00:07:15,610 --> 00:07:21,150
I'll go over my solution to this
challenge in the next video.

