Welcome back.
In this video I want to show you how to debug logging works.
So the thing is what we're doing right now is we are just displaying something onto the console.
For example, the error message, we're displaying it directly onto the console.
And in our case the console is the actual final application.
So the project or the program that we're delivering to our customers, so to speak.
Right.
But what if we want to make sure that the customer doesn't see the errors, but we want to see them
so that we can modify our code accordingly during the debugging and during coding in general during
the development, so to speak.
So therefore we can use debug dot write line.
Let me quickly show you what I mean with this.
So let's say I want to say okay the main method is running.
So this is a very simple example.
It really isn't like a real world example where this would make sense.
But this allows me to say something like main is running.
So main method is running.
Okay.
Now if I run this code we will see that inside of my output here at the bottom right in the debug,
I can see my main method is running.
So this debug is one way of logging whatever you want to be logged, whether it's the value of a variable,
whether it's to make sure that a certain flow is running.
So your data or your the execution of your code is following a certain flow, right?
A control flow, so to speak, or whether it's that you want to log errors but you don't want to display
them to the user, or you want to display them to the user in one way, but want to display the details
to you in a more sophisticated way.
So, for example, if we have this error here, this catch exception error error message, right.
The message that we send to the user is very simple.
So here we say error attempted to divide by zero.
So this is something that the user our customer so to speak who runs this Excel file is supposed to
see.
So now the user knows okay in the future I should not enter zero for example.
But if we want to know more about what actually happened about the error, now obviously we can do debugging
where we add the debugging breakpoint here and so forth.
But we can also just run it directly and see the results on our log.
Okay.
So therefore what I'm going to do is I'm going to say debug dot write line.
And here I'm going to use the exception dot stacktrace.
So let's run this again.
And we leave the message that the user gets as what it was.
But at the same time we're going to look at our debugger here.
So here it says exception thrown system divide by zero exception in try catch exceptions DLL.
So that's the file that our program is automatically generating when we are executing the code.
So the stack trace would be maybe not enough information.
Maybe we want to have more information.
So let's make a string out of this entire statement.
So out of this exception and let's see what kind of information we're going to get this time when we
divide by zero.
So attempted to divide by zero.
Let's look at our output here inside of our debugger.
And you can see it says system divide by zero attempted to divide by zero add try catch exception program
main string.
And then it says where this error occurred.
So it says it occurred in line 19 okay.
So now I can go ahead and fix whatever happened in line 19 and see okay well divide by zero exception.
So how can I get my way around it now.
Actually I could check.
Does the user actually enter a whole number?
If so, then uh good.
If not, then let the user know about the problem.
All right, so this just gives you another tool that allows you to do your debugging okay.
So sometimes you want to use breakpoints because you know exactly where you want to check something.
But sometimes you just have such a complex application where using breakpoints every time, like everywhere,
would take or make it super slow and it would take forever.
But using the debugger, it allows you to see the state of a variable, the state of your project in
general of the error that occurred, or whatever the exception that occurred.
You can display all of that directly in the output in your log.
That then allows you to see it and improve your application basically.
So for development this is crucial.
So now the other thing that is really good about the debug feature is that this will not be executed
in the production app.
So it is executed during our.
Debugging, which we do inside of our application by hovering over here.
So now if I, for example, were to run it without debugging, let's look at our log and let me divide
by zero.
Here I get the error as a user.
But here in the output I don't see it.
I can change it to debug.
So show output from debug.
And you see I don't see anything.
That's because I'm not in debug mode.
That's because I am in build mode, which in the end is the production mode so to speak.
So what the end user will see.
And this just means that whatever execution time is required or processing power is required to run
this line, it will not be required during production.
So it will only be required during debugging.
So this next line is only executed during debugging.
Okay, so that's another really cool thing about this logging feature.
Now there are a bunch of other logging features out there as well.
So some of them allow you to even log into a file.
So where you can then see the errors directly in a file.
And that is very useful.
For example, if users can run into errors and you need this information in order to then update your
app, because then you see which kind of errors users ran into, and otherwise you might not see it
because you are testing it your way.
But users are using your application in different ways, and then using another way of logging would
be crucial.
Okay, but this is just for you during debugging.
Really powerful and mini tool so to speak.
I'm a small tool that just does the job for you.