180 likes | 313 Views
BİL527 – Bilgisayar Programlama I. Debugging and Error Handl ing. Contents. Debugging and Error Handling Debugging feature in Visual Studio try … catch … finally. Debugging and Error Handling. Error Types. Syntax Errors
E N D
BİL527 – Bilgisayar Programlama I Debugging and Error Handling
Contents • Debugging and Error Handling • Debugging feature in Visual Studio • try … catch … finally
Error Types • Syntax Errors • Forgetting semicolon, not closing parentheses or curly braces, writing variable name incorrectly, etc. • Program is not compiled • Examine the compiler errors and correct the codes • Logical Errors • Program is compiled • Program does not give the desired output • Debug your program!
Debugging in Visual Studio • Put breakpoints in your code • F9, click at the beginning of a line, Debug menu • Start your program in Debug mode • F5 (not Ctrl-F5), Play arrow in toolbar, Debug menu • Press F10 for single step • Press F11 to enter a function • End debugging with Shift-F5 or Stop in toolbar
Monitoring Variable Content • Move cursor an a variable while program is being debugged • Follow the variables in the Localsand Autoswindows • Add variables into the Watchwindows • Use the Immediate Window • Write the name of the variable and press Enter • You can call functions and expressions too
Configurations • Debug • Extra symbols are included in the executable file so that it can be debugged • Runs slowly • Executable file size larger • Release • Runs faster • Executable file size smaller • Use this configuration on the last build (before distributing your program)
Debugging Without a Debugger • A debugger (e.g. Visual Studio) makes debugging easier • If you don’t have an IDE, you can use Console.WriteLine() to debug your programs • Just call Console.WriteLine() to print variables or some useful messages • Don’t forget to remove those lines in the last build
Debugging in Nonbreaking (Normal) Mode Console.WriteLine("MyFunc() Function about to be called."); MyFunc("Do something."); Console.WriteLine("MyFunc() Function execution completed.");
Some Advanced Topics • Debug.WriteLine() / Trace.WriteLine() • Debug.WriteLineIf() / Trace.WriteLineIf() • Debug.Assert() / Trace.Assert() • using System.Diagnostics; • You can set number of hits and hit condition for breakpoints • Just right-click on the red dot of the breakpoint
Error Handling (try..catch..finally) • While running a program through the IDE (i.e. during the development step, or Debug mode), you receive an error message if an error occurs. • However, if your program runs in the final version (or Release mode), an error causes your program to terminate. • For these cases, you may prefer using structured exception handling (or try..catch..finally)
Sections of the try Structure • try: The try section is where you place code that might cause an exception. • catch: Code within the catch section executes only when an exception occurs. • finally: Code within the finally section occurs when the code within the try and/or catch sections completes. This section is where you place your cleanup code—code that you always want executed, regardless of whether an exception occurs.
try Example int number = int.Parse(Console.ReadLine()); int result = 100 / number; string str = result.ToString(); An error may occur if the number is zero! An error may occur while converting a text into an integer!
try Example – Solution 1 try { int number = int.Parse(Console.ReadLine()); int result = 100 / number; string str = result.ToString(); } catch { Console.WriteLine(“An error has occurred!”); }
try Example – Solution 2 try { int number = int.Parse(Console.ReadLine()); int result = 100 / number; string str = result.ToString(); } catch (ArgumentException ex1){ Console.WriteLine(“An ArgumentException error occurred!”); } catch (DivideByZeroException ex2) { Console.WriteLine(“The divisor can’t be zero”); } catch (Exception ex) { Console.WriteLine(“An unspecified error occurred. The details is: ” + ex.Message); }
try Example – Solution 3 int result = 0; string strResult; try { int number = int.Parse(Console.ReadLine()); result = 100 / number; } catch (Exception ex) { Console.WriteLine(“An unspecified error occurred. The details is: ” + ex.Message); } finally { strResult = result.ToString(); }
try Example on Database Operations try { … // Open database … // Make some operations on the database } catch { … // Error handling statements } finally { … // Close the database }