In this lecture, we will be dealing with exceptions and also we will fix a minor bug.
We will be using try and catch blocks in order to deal with the exceptions and the minor bug is in case
the given command doesn't contains any space character our parser will throw an error.
We will be fixing it by using an if statement.
And also, if any exceptions occur, you will be returning the message as string, the error message as string,
this information will be used for determining why our command didn't work.
If you are new to these concepts, please learn try and catch blocks before keeping continue.
So let's start coding.
Inside the operations class
we will be using try and catch blocks in all of the functions.
Let's start with download file function.
Encapsulate all of your code inside try block
and catch any possible exception by using catch statement, catch block.
In order to get the error message, we will be using an instance of the exception class.
Let's call it e and in order to return the error message, we will be using the message property of
the exception class and we need to convert its data type to string.
So we will be using the tostring method.
Since we are returning a string, we need to change return that type of the function, it will be
string and also we need to create a success message.
Let's call it "file has been downloaded" and append savepath next to it.
Do the same thing for all of the functions.
Encapsulate your code inside try block and catch any possible exception using catch block.
And create the success message.
And also, we need to change the return data type of the function, it will be string.
And for enumworkingdirectory function, use try block and encapsulate all of your code inside
try block and catch any possible exception using the catch block.
And lastly, the executecmd function.
Use try block
and
catch any possible exception using catch block.
So we are done with dealing with exceptions.
Let's fix our minor bug.
Go back to your commandparser function.
If the given command
doesn't contain any space character, this split function will throw an error.
To fix this situation, we will be using an if statement if the given command contains any space character
we will be performing split operations else
We will be initializing the value of the command as cmd variable's itself.
So let's perform split operations inside the if block
and
let's initialize the first value of the command variable and argument variable as empty strings.
So if the given command contains the space character
We will be changing their values according to the split functions.
And in case the cmd variable does not contain any space character, we will be initializing
the value of the command as the cmd variable's itself and argument will be empty.
So we have completed our tasks.
Let's test our code. Go back to your program.cs
and call commandparser function.
Let's use the ls command with argument as nonexistent directory.
Let's call it C:\Windowssse and let's see let's see the error message.
Oops, there's an error exists, let's fix that first, where is the error?
Oh we need to use the return function.
Lets try it again.
As you can see, the error message has been returned, "could not find a part of the path" and let's
try to call enumworkingdirectory function with a valid path.
Let's see.
Yep, it worked.
As you can see, we have listed all of the files and folders under the C:\Windows directory.
One more improvement can be performed here.
If the given command only contains the ls keyword, we need to enumerate our current working directly
to do so, go back to your enumworkingdirectory function
and if the given path is empty
If give given path is empty, intiliazie it's value as our current working directory to do so, we will
be using an instance of the general info class.
An instance it is an instance of the general info class and we will be using cdirectory variable,
this variable holds our current directory information.
So if the given path is empty, we will be using our current working directory as given path.
Let's test or code.
Yep, perfect, as you can see, we have listed files and folders under our current working directory,
so that's it for this lecture.
See you in the next one.