1 00:00:00,550 --> 00:00:06,220 In this lecture, we will be dealing with exceptions and also we will fix a minor bug. 2 00:00:07,960 --> 00:00:15,820 We will be using try and catch blocks in order to deal with the exceptions and the minor bug is in case 3 00:00:15,820 --> 00:00:21,110 the given command doesn't contains any space character our parser will throw an error. 4 00:00:22,000 --> 00:00:24,490 We will be fixing it by using an if statement. 5 00:00:27,150 --> 00:00:34,590 And also, if any exceptions occur, you will be returning the message as string, the error message as string, 6 00:00:35,760 --> 00:00:40,350 this information will be used for determining why our command didn't work. 7 00:00:42,200 --> 00:00:47,990 If you are new to these concepts, please learn try and catch blocks before keeping continue. 8 00:00:48,930 --> 00:00:50,550 So let's start coding. 9 00:00:53,460 --> 00:00:55,140 Inside the operations class 10 00:00:56,820 --> 00:01:01,020 we will be using try and catch blocks in all of the functions. 11 00:01:01,950 --> 00:01:04,410 Let's start with download file function. 12 00:01:06,150 --> 00:01:10,920 Encapsulate all of your code inside try block 13 00:01:13,470 --> 00:01:19,140 and catch any possible exception by using catch statement, catch block. 14 00:01:21,620 --> 00:01:27,470 In order to get the error message, we will be using an instance of the exception class. 15 00:01:28,970 --> 00:01:37,070 Let's call it e and in order to return the error message, we will be using the message property of 16 00:01:37,070 --> 00:01:42,730 the exception class and we need to convert its data type to string. 17 00:01:42,860 --> 00:01:45,700 So we will be using the tostring method. 18 00:01:48,890 --> 00:01:55,840 Since we are returning a string, we need to change return that type of the function, it will be 19 00:01:55,850 --> 00:02:01,820 string and also we need to create a success message. 20 00:02:03,450 --> 00:02:12,000 Let's call it "file has been downloaded" and append savepath next to it. 21 00:02:15,510 --> 00:02:17,940 Do the same thing for all of the functions. 22 00:02:19,730 --> 00:02:29,600 Encapsulate your code inside try block and catch any possible exception using catch block. 23 00:02:36,520 --> 00:02:38,080 And create the success message. 24 00:02:45,230 --> 00:02:50,330 And also, we need to change the return data type of the function, it will be string. 25 00:02:54,300 --> 00:03:03,620 And for enumworkingdirectory function, use try block and encapsulate all of your code inside 26 00:03:03,640 --> 00:03:10,560 try block and catch any possible exception using the catch block. 27 00:03:14,600 --> 00:03:17,510 And lastly, the executecmd function. 28 00:03:19,870 --> 00:03:21,270 Use try block 29 00:03:26,650 --> 00:03:27,370 and 30 00:03:29,350 --> 00:03:34,620 catch any possible exception using catch block. 31 00:03:37,850 --> 00:03:40,340 So we are done with dealing with exceptions. 32 00:03:41,210 --> 00:03:42,980 Let's fix our minor bug. 33 00:03:46,080 --> 00:03:48,120 Go back to your commandparser function. 34 00:03:49,450 --> 00:03:50,680 If the given command 35 00:03:51,570 --> 00:03:57,080 doesn't contain any space character, this split function will throw an error. 36 00:03:58,900 --> 00:04:09,400 To fix this situation, we will be using an if statement if the given command contains any space character 37 00:04:10,660 --> 00:04:14,620 we will be performing split operations else 38 00:04:17,090 --> 00:04:23,690 We will be initializing the value of the command as cmd variable's itself. 39 00:04:27,260 --> 00:04:32,470 So let's perform split operations inside the if block 40 00:04:45,280 --> 00:04:45,910 and 41 00:04:48,840 --> 00:04:55,880 let's initialize the first value of the command variable and argument variable as empty strings. 42 00:04:56,940 --> 00:05:01,290 So if the given command contains the space character 43 00:05:03,180 --> 00:05:09,630 We will be changing their values according to the split functions. 44 00:05:10,290 --> 00:05:18,930 And in case the cmd variable does not contain any space character, we will be initializing 45 00:05:18,930 --> 00:05:23,100 the value of the command as the cmd variable's itself and argument will be empty. 46 00:05:25,550 --> 00:05:28,820 So we have completed our tasks. 47 00:05:29,630 --> 00:05:34,100 Let's test our code. Go back to your program.cs 48 00:05:35,570 --> 00:05:38,240 and call commandparser function. 49 00:05:39,950 --> 00:05:48,050 Let's use the ls command with argument as nonexistent directory. 50 00:05:49,550 --> 00:05:56,570 Let's call it C:\Windowssse and let's see let's see the error message. 51 00:06:03,220 --> 00:06:11,070 Oops, there's an error exists, let's fix that first, where is the error? 52 00:06:19,410 --> 00:06:25,020 Oh we need to use the return function. 53 00:06:28,640 --> 00:06:29,630 Lets try it again. 54 00:06:33,610 --> 00:06:42,160 As you can see, the error message has been returned, "could not find a part of the path" and let's 55 00:06:42,160 --> 00:06:49,140 try to call enumworkingdirectory function with a valid path. 56 00:06:51,580 --> 00:06:52,190 Let's see. 57 00:06:54,070 --> 00:06:54,970 Yep, it worked. 58 00:06:55,120 --> 00:07:03,540 As you can see, we have listed all of the files and folders under the C:\Windows directory. 59 00:07:05,830 --> 00:07:09,490 One more improvement can be performed here. 60 00:07:12,130 --> 00:07:21,190 If the given command only contains the ls keyword, we need to enumerate our current working directly 61 00:07:22,330 --> 00:07:27,220 to do so, go back to your enumworkingdirectory function 62 00:07:32,770 --> 00:07:36,280 and if the given path is empty 63 00:07:39,090 --> 00:07:49,350 If give given path is empty, intiliazie it's value as our current working directory to do so, we will 64 00:07:49,350 --> 00:07:52,950 be using an instance of the general info class. 65 00:07:57,340 --> 00:08:07,900 An instance it is an instance of the general info class and we will be using cdirectory variable, 66 00:08:08,890 --> 00:08:11,700 this variable holds our current directory information. 67 00:08:12,040 --> 00:08:18,610 So if the given path is empty, we will be using our current working directory as given path. 68 00:08:20,080 --> 00:08:21,190 Let's test or code. 69 00:08:30,360 --> 00:08:38,330 Yep, perfect, as you can see, we have listed files and folders under our current working directory, 70 00:08:39,150 --> 00:08:40,840 so that's it for this lecture. 71 00:08:41,370 --> 00:08:42,410 See you in the next one.