1 / 18

BİL527 – Bilgisayar Programlama I

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

osman
Download Presentation

BİL527 – Bilgisayar Programlama I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BİL527 – Bilgisayar Programlama I Debugging and Error Handling

  2. Contents • Debugging and Error Handling • Debugging feature in Visual Studio • try … catch … finally

  3. Debugging and Error Handling

  4. 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!

  5. 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

  6. 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

  7. 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)

  8. 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

  9. Debugging in Nonbreaking (Normal) Mode Console.WriteLine("MyFunc() Function about to be called."); MyFunc("Do something."); Console.WriteLine("MyFunc() Function execution completed.");

  10. 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

  11. Error Handling

  12. 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)

  13. 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.

  14. 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!

  15. 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!”); }

  16. 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); }

  17. 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(); }

  18. try Example on Database Operations try { … // Open database … // Make some operations on the database } catch { … // Error handling statements } finally { … // Close the database }

More Related