Hello everyone, my name is Atharva and we are continuing this C programming course in which, in the last video we had discussed about typecasting.
To understand this, we had taken one basic example where we added two integer type values and the output sum of that addition was , even though that was an integer but what we did is, we stored that integer value in a float type variable.
When we did this, that value also got converted into float, it got changed into float.
So, this type of data type conversions which are there, how those can be done that we had discussed in the last video.
Now, we are coming on to a new topic which is called handling the errors.
What is the meaning of handling the errors? We will understand that and start with what exactly is an error.
So, here you can see, the program is understood by the compiler one by one and it executes it.
So, as and when he is reading the lines one after the other, let's suppose we do this our if statements which are there if we don't write I f and we write i f f (double f) by mistake, will the compiler understand that what we are trying to say with i f f, so our compiler will not understand this thing.
And that's why what he will do is consider it as an error and it will stop our compilation process completely.
And whoever is our user who is using this program, he doesn’t like this abrupt break or abrupt ending of the program.
So, what we can do is if we tweak this error in such a way that suppose if any of the errors is going to occur and we know beforehand that some error can come.
So, if we give some set instructions early on in our program because of which the compiler will not stop the entire compilation process.
And it handles it very easily.
So that our user’s experience is not spoiled.
Can we do all these things? We can definitely do it with the concept called error handling.
So, we will move forward and see how we can successfully get this inside our program.
So, let's start with the very first question, what is an error? So, we have already seen it, still we will read the definition and understand what it is exactly trying to say.
“Whenever we make a mistake in the code, and the compiler doesn't understand it, it gives an error.”
So, what are they trying to say here? Whatever is our program in that if we are committing any mistake, at that time our compiler doesn't understand that thing.
And because of this it generates one error.
Now these errors can be of different types.
Somewhere we can make mistake in writing the syntax.
So that will become a syntax error.
Somewhere it so happens that we make a mistake in the logic, which means we divided any value with zero that will become infinity and infinity cannot be represented here.
So, if we write these kinds of logical errors in our program, our compiler will consider these as an error.
So, this was about the error ,that is how error comes.
Now we will be seeing how this error can be handled.
Because of which the compilation process doesn't stop at once.
And it continues and it can be handled easily.
So, we will move forward and see about a new variable which is called E R R N O.
So, what is an E R R N O, we can even call it an error number.
So, what is this error number in actuality, we will see that.
So, “error number is a global variable which can be used to identify which type of error was encountered during function execution based on its value”.
So, what is it trying to say here? Let's understand.
Suppose our compiler doesn't understand any line of the program and it stops our compilation process and we come out of the entire compilation process.
So, what happened here was that we got to know that there is some error in the program.
But what exactly is that error? What mistake has happened from us? How will we get to know that? We will get to know that with the name of one variable which we call as E R R N O, which means error number.
So, what happens in this variable? There are different values that get stored and who stores these values? The compiler itself stores these values, like in this it has got stored 1, 2, 5, 11, 13, different types of values like these, gets stored in it.
So, now that it is stored, what is the benefit that we will be getting? How will we get to know what is the error that has come? So, for every value there is a corresponding text that has been already defined in the C program, which means in the C programming language.
It means if any value comes inside this variable and gets stored.
We can check what this value actually represents, what text it is representing.
And we can print that text in our program here.
And with that text we get to understand what is the exact problem in the program.
So, in this way, we can stop the error in our program easily in a controlled way.
And we can easily break it, which means it will not happen that the program has suddenly stopped.
And we don’t even know what is the actual problem in the program?
So here we saw that there is, E R R N O named variable in which there are few values which are stored and there is some meaning of those values which is already defined in the C programming language.
So how we can print these values we will see that going ahead.
This value we can extract in two ways or in two functions and with the help of print f statements we can get them printed.
So, we will see that both these functions are the P error function and STR error function which is there.
How does this work and what is their work? So, perror and strerror functions are used to display the text message of errors generated.
So, our e r r n o variable which is there, the values that come inside it.
It converts them into a text message.
And then that text message gets printed on our screen because of which, we will get to know exactly what is the problem.
So, we will quickly understand them one by one.
We will first see what does the P error function do? What is its exact work? So, “the P error function displays the string we pass to it followed by a colon and then the text of the current E R R N O value.”
So, what happens here basically, like we have seen that in E R R N O there is one value that is stored.
And corresponding to that value, there is a text which is already defined in C programming language.
So, what does our P error function do? It takes one argument and that argument is in double quotes.
So basically, we can say that we will write a string in it or a sentence in it, which would be relating basically to that particular error.
So, what will happen here in its argument, we pass one string which will get printed as it is on our console or on our terminal.
And before that this P error function will automatically put a colon and after that, after putting the colon… so first of all the string came which we passed on our own.
After that one colon has come and after the colon the value stored in E R R n o.
So, what was the text actually hidden in that value? So, in corresponding to that value, what text is stored in the C programming language? So, that text is extracted here by our P error function.
And it gets that text printed over there.
So, this was the theoretical thing.
We will also see it practically but before that we will understand the STR error function that is there, how is it different from our P error function? So, here we have to give the text on our own which gets printed together with our current E R R N O value.
So, after the P error function, we are seeing the S T R error function.
And what does this strerror function do? It returns a pointer to the textual representation of the current E R R N O value.
So, what does this do? It prints the same text, which is corresponding to the e r r n o value.
But we don't pass any statement on our own which will get printed along with the error number.
So, this is the difference between P error and STR error.
Now we will move ahead and we will understand these both, e r r n o function practically.
So, for that we will directly come on to our Visual Studio code.
And here we have already made one program which is related to file handling.
And we will see here how very easily, which means whatever error will be generated in it, how we can easily counter that.
So, this program will open in the read mode, okay.
But we all know that when we are opening any file in the Read mode, if that file exists then it will be open in read mode if it doesn't exist then it will throw an error and that file will not open because that file doesn't exist.
But if we open the same thing in write mode, even if the file was not available, it would have generated the file and it would have opened it in a write mode.
But here since we are opening it in read mode, if our file doesn't exist then our compiler will generate one error and how we will handle that error that is what we are going to see here.
So, first of all we will talk about our preprocessor command.
So, our first preprocessor command is hash include stdio dot h.
So, for standard input and output we have imported the stdio dot h header file and after that you can see that there is a second preprocessor command, where we are also importing errno dot h file.
So, why are we importing this because we all know that we have to use err no variables over here.
That is why we will be importing it here.
Along with that we are seeing that we are importing string dot h file as well because we are going to make use of strings as well over here.
So, this was about our preprocessor commands.
Now we will straight away go into our main function and we will see what exactly is happening over here.
So, here we have made one file pointer which is called F ptr.
And in F ptr basically, we have used F open function over here and we are telling our compiler that compiler, what you can do now is nofile dot txt named file which is there, at this particular location.
You open it in read mode and give it to us.
So, what will the compiler do? It will search for the file named no file dot txt in this particular location but if you see here, there is no file named no file dot txt that is not stored over here for now.
So, we can even see it once.
If we move towards to N, there is no file which is called no file dot txt.
So that's the reason why this particular program will generate an error for us.
So, this was about opening the file, in read mode and it will not open.
When it will not open.
Then what is going to happen.
That is what we will be seeing here.
Let's suppose that this is not opening over here.
It is not opening and after that this print F statement that is written over here, that will start getting executed and we will see one by one how these values get printed.
So, the first statement is going to return the value and where is that value coming from? It is coming from the E R R N O variable.
Which basically we had considered from here.
So, in the E R R N O variable, there must have been a value that was stored related to this particular problem and this particular error which was to open a file in read mode, but that file itself doesn't exist.
But some value related to that file has been stored in the E R R N O variable.
So, what is that value exactly that we are printing with the help of print F statement.
After that if we talk about the next print F statement.
Here it is saying that the error message is given as…here we have made use of the strerror function and we had seen in the previous slide how this str error function is there.
It basically represents one pointer on textual data in particular E R R N O the value which was stored, corresponding to that.
Here since it is handling the E R R N O function.
So, we have given E R R N O in the argument and we have used the str error function over here.
Basically, this str error whichever is the function, will return it, that will get printed, with the help of print F statement in front of what? In front of error messages given as.
This was about our str error function.
Now, we will see our second function, P error.
So, how does this work? We had seen that we will pass a string or a sentence in P error, which you can see that we have already passed in double quotes, which is saying message from P error.
So, this is our string statement, now after this we have seen in P error, it also adds one colon and in E R R N O, the corresponding text that is there basically.
It prints that before the colon and gives it to us.
So, this was about our err no, about str error function and about the P error function.
So, we have seen all these things and we have also got to know that these lines will give an error here as it has been opened in read mode.
And here there is no file named nofile dot txt.
So, there is no file.
That's why this particular line will give us an error.
So how will we handle this error? We will handle it with the help of this statement.
And if we don't write these statements, we will not understand exactly what was the error.
So, we have saved it and what we will do now is, we will run it in a different way.
We can also right click and click on the run code itself.
But there is one more way through which we can run this particular program.
We will also see that way over here, how it can be accessed through one terminal or one command line.
So here you can see that a few circular and triangular marks are made.
This basically represents all these boxes, which means of the problem, from the output one, the debug console and the terminal one.
As soon as you open it, one box will open below which we called a console or terminal as well.
So, here you have to click on the terminal option and you will see the terminal is getting open over here.
Here as soon as the terminal gets opened.
You can see that your path is basically set over there, where your program is stored.
Since my program is stored in my desktop under C folder, so the same path has come over here.
So, what do I have to do now? Now I have to write a few lines.
I have to write a few commands with the help of which my compiler will know that I have to execute this program.
Here since we are using a GCC compiler.
So, what we will do is we will use a GCC command and before that, we will write our file’s name which is “Error handling dot C”.
So here you can see that since the file’s name “Error handling dot C”, so I have given space before gcc and I wrote “Error handling dot C”
Now I will press enter, with this statement my “Error handling dot C” program, it has been compiled.
Now what I have to do is, I have to run it now.
What does compile mean? Our compiler understood the entire program, read it.
And as soon as it understood everything, he compiled everything.
You can see here that as such no error has been printed over here.
Because we have made use of a little bit of error handling over here.
So, here the compiler is not facing any such kind of difficulty.
But still it has put such a value in err NO, with which we will understand that even if the problem is coming in the program.
What exactly is that problem? So now we will see our second step of running our program which is simply writing the name of our program file over here.
So, the first step was the GCC space file’s name because of which our program gets compiled and as soon as we start seeing the second path again over here.
We write our file name only in this way.
We don't write any extensions, which means we don't put dot C before, and simply press enter.
So, you can see that there is no error that has been performed over here or there is no error that has been displayed over here.
Basically, there are few statements which have got printed over here.
Which obviously we had got printed over here.
So first of all, we got the print F statement printed, which was saying the value returned from ERR NO and that value is 2.
So, you can see here what is the value that was stored in ERR NO, 2.
What does it mean that the value has been stored.
So, what does it mean by the value stored, there was some error that had come to our compiler.
So, what was it that we will understand in the next line? So, the next line is saying the error message is given as and from where will we extract that error message? From the STR error function, in which again we will give ERR NO variable and here the text will get printed.
So, as soon as we saw the second line, you can see here that the statement that got printed, “no such file or directory” got displayed over here.
So, with this we understand that the file that we wanted to open over here, there is no file or directory available with this name.
So, while reading this line, we will understand that there is some problem in the file.
And we will check if the file of this name is there with us.
If not, we will understand that there was a problem exactly here.
So, with this line or the statement, we got to understand that in our program where exactly the error was faced and even that error, we have not extracted through the compiler.
Compiler compiled everything successfully.
But later with the help of variable ERR NO, we understood our error and analysed it.
Now if we see the third line, that is our P error function.
And you can see that inside it is the message from the P error statement that I had written that also has got printed as it is and before that automatically the colon has been put.
You can see here that here we had not put the colon but still the colon has been put and after the colon the O 2 value that was there in errno.
What was the exact text corresponding to that value? Exactly that text has come here and got printed, which is saying “no such file or directory”.
So somewhat in this way, our compilation process has not been broken and we also got to know about our errors.
And this we call as error handling.
How one error can be handled and the compilation is not stopped and along with easily handling it we also get to know where exactly the error had come from and where there was some problem in our program.
So, this was about our error handling program.
And we got to know how to do error handling.
How the text can be extracted in that error.
So, all these things we have learned in this video, understood and we studied them carefully.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
In the next video, we will be discussing recursive functions.
We have already seen the functions but what are these recursive type functions exactly, that we will be discussing in the next video.
Till that time, all of you practice recursive functions.
Till that time, thank you so much.
Very happy 😄😁
Very happy and greatful, thank you , great course.
Super for poor students to learn software course languages TQ