Results 1 to 4 of 4
  1. #1
    NextGen1's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Not sure really.
    Posts
    6,312
    Reputation
    382
    Thanks
    3,019
    My Mood
    Amazed

    Handle Your errors already...

    Deal with your Errors!

    I have been browsing around the forums (mpg.net, so many new developers there...) and have come across a lot of programs (most) developed (you can tell) by a new developer, When I run (some of) these programs
    I tend to get errors ( A Huge! pet peeve of mine), So I Have taken some time and decided to write a Article (or a Tut of sorts) on how to handle these errors..

    Error Types:

    Syntax Error (design error)

    These errors happen when vb.net doesn't understand what you are telling the code to do. These are easy to Find in VB.NET, because you get a very annoying blue wavy line underneath the issue (don't ignore these...)

    Runtime errors

    This error happens when your program is running. This can happen when your code is trying to do something it "shouldn't be doing" , I personally hate getting a runtime error when testing someone else's software for them. How can you get this error? Writing to a file that doesn't exist , opening a file that doesn't exist and so forth. It's your job as the programmer to plan ahead and determine how you can handle this (Such as using FileOpenDialog)

    Logic errors

    Logic Errors occur when the result (or part of the code) landed an answer you didn't expect.

    How to handle (Debug Like Hell)

    Lets first start by learning how to deal and prevent Syntax errors (or design errors)

    Note: (For screenshot purposes I have created a blank project, I have added no controls and I am viewing code for Form1)


    [IMG]https://i111.photobucke*****m/albums/n121/golmor/textboxerrorsyn.jpg[/IMG]


    Note: Those Who C&P will have this problem a lot, because when you copy and paste a code without adding the components, you will receive a designer error.

    Solution: Hover your mouse over the blue wavy line, in this case you will get a "Tip" saying "Declaration Expected" In this case, simply add a texbox to the form, be sure the name you have given (or copied.............) matches the name of the component.

    In some cases you will need to use dim to be sure it is declared (if that is the reason)

    let’s use this sample code.

    Code:
    gettext = textbox9.text
    Let’s say textbox9 is a real component on the form (right name and all) and gettext was underlined, what would you do ?

    There is a right way and a wrong way to use dim (I'll touch on it briefly, but I know I seen a tut out here on mpgh.net that goes into details about dim)

    [IMG]https://i111.photobucke*****m/albums/n121/golmor/dim123.jpg[/IMG]

    What is wrong with the code? Take a second and try to figure it out before moving down...
    .
    .
    Answer = Dim should be place above the the code to be declared, and also must be placed ( Also learn Public and Private, if you want a tut on that, PM me) and when you use dim, you may need to declare it "as" something. (In this case, I’m using string)

    The code should be

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim gettext as string
    
            gettext = "designer"
            TextBox9.Text = gettext
    
            
        End Sub
    Final Screenshot, Don't ignore these either

    [IMG]https://i111.photobucke*****m/albums/n121/golmor/errorlist.jpg[/IMG]

    Note: You can publish with designer errors, so don't ignore them.


    Runtime Errors:

    Ever debug your application and get a error message mid code, or mid process, that’s a runtime error, a error that happens in code while it is (as name implies...Running!)

    I’m going to use the (with Rnd() and Cases and Num and clnt () being so common among new learners) overflow error

    [IMG]https://i111.photobucke*****m/albums/n121/golmor/Overflow.gif[/IMG]

    Note: If you try and divide by 0 you will get an overflow, if you create a random number between 1-6 in order to generate a random response via case and the number is
    not a "possible case" you will get a overflow.

    Note: Deal with runtime errors as they occur, Do not overlook these, even if a debug shows up clean, reason being... Your user may stumble upon this part of the code (where maybe you overlooked it, and this happens a lot, the program will (almost) always crash.

    Note: if you ever thing a mathematical equation can result in zero use + 1

    Example of + 1 and when to use it, let’s say you do

    Code:
    dim r 
    r = rnd() * 4
    ask yourself before you continue , is it possible to land a 0 result (null) , the answer is yes, 0(can be the random number) * 4 = 0, this may never happen to you
    during debugging, but may happen to a user

    your code should be ( to prevent this)

    [code]
    dim r
    r = rnd() * 4 + 1
    [code]

    Now if the code lands a 0 , it will add 1 , and the result will = 1

    How to "trap" in order to deal with runtime errors

    VB.NET has a class that deals with runtime errors. "Exception"

    Note: We use try to catch runtime errors and determine the appropriate action to deal with them.

    Code:
    Try
    ' to do something
    msgbox "all is ok"
    
    Catch ex As Exception
    
    msgbox (ex.message) 'Displays a message box with the error..like "resulted in 0" or "File not found"
    
    End Try
    or the cleanup method (which is optimal according to msdn, however , it's a life saver to me)

    Use Finally, which will work out the cleanup and allow a action to continue regardless of an error

    Code:
    Try
    
    Catch ex As Exception
    
    Finally
    
    End Try

    If you know what is causing the error you can use try to add different handlers.

    Code:
    Try
    ' something
    
    Catch ex As Exception
    ' msgbox (ex.message)
    
    Catch ex As System.IO.FileNotFoundException ' for file not found
    
    msgbox (ex.message) ' or anything you like
    
    End Try

    Logic errors

    Try this, open vb.net, copy and paste this code (add a textbox)

    Code:
    'do this in form load for simplicity
    
    Dim a As Integer
    Dim b As Integer
    Dim total As Integer
    
    a = 5.5
    b = 3
    total = x * y
    TextBox1.Text = total
    You would assume the answer would be 16.5, but you may be surprised, the answer is 16!!!!..!!!

    Logic errors are hard to find, program doesn’t crash, so... not runtime, no wavy blue wavy lines, so...not a syntax error, so what do we do to fix the problem, in this case.... lots and lots of debugging and breakpoints.

    Adding a breakpoint is as simple as click next to a line of code on the grey area to the right, what this will do is (in a sense) Pause the code at a particular part of the code (chosen)

    [IMG]https://i111.photobucke*****m/albums/n121/golmor/break1.jpg[/IMG]

    Run your application, when the program hits your breakpoint your application will "break" (pause) hover over the breakpoint (highlighted, image below) you will notice some details, use those details to determine any issues you may be having, (debugging)

    Press F10 on your keyboard to step through one line at a time, being sure to look for any inconsistencies .

    ......................................


    Ok , that's my error handling article , Use it, Keep your code clean.
    Last edited by NextGen1; 01-05-2010 at 02:12 AM.


     


     


     



    The Most complete application MPGH will ever offer - 68%




  2. The Following 2 Users Say Thank You to NextGen1 For This Useful Post:

    Blubb1337 (01-28-2010),Zoom (01-14-2010)

  3. #2
    Youngie1337's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    United Kingdom!
    Posts
    8
    Reputation
    10
    Thanks
    0
    Thanks for this, I've just moved to .NET so this is a helpful post for me.

  4. #3
    NextGen1's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Not sure really.
    Posts
    6,312
    Reputation
    382
    Thanks
    3,019
    My Mood
    Amazed
    Quote Originally Posted by Youngie1337 View Post
    Thanks for this, I've just moved to .NET so this is a helpful post for me.
    Good, we need more VB members in this section, it's dying out a bit


     


     


     



    The Most complete application MPGH will ever offer - 68%




  5. #4
    Zoom's Avatar
    Join Date
    May 2009
    Gender
    male
    Location
    Your going on my 24/7 DDoS hit list.
    Posts
    8,552
    Reputation
    127
    Thanks
    5,970
    My Mood
    Happy
    This should be sticky!
    -Rest in peace leechers-

    Your PM box is 100% full.

Similar Threads

  1. SOLVING TO YOUR ERROR
    By UL7RA in forum CrossFire Help
    Replies: 30
    Last Post: 06-07-2011, 02:19 PM
  2. Replies: 7
    Last Post: 05-03-2011, 07:16 PM
  3. compile error :/ from [Writing your own C++ Trainer]
    By FantaBrause in forum C++/C Programming
    Replies: 7
    Last Post: 07-06-2009, 11:09 PM
  4. [Solution]-Combat Arms Already Running Error
    By Drown2008 in forum Combat Arms Hacks & Cheats
    Replies: 8
    Last Post: 08-21-2008, 05:51 PM
  5. I get error key code already in use some one can help???
    By aprill27 in forum Call of Duty 4 - Modern Warfare (MW) Hacks
    Replies: 6
    Last Post: 01-11-2008, 09:37 PM