locked
How to add predefined errors into program? RRS feed

  • Question

  • My question is how would I add a predefined error to this program I did? Thanks!

    Module Module1 'VB.NET Final Project 12/30/14 . Option 3: Write a program to run two separate threads printing numbers or words (It can do both) in ascending and descending orders. The words have to be given by the user.

        Dim Ascending As System.Threading.Thread = New Threading.Thread(AddressOf PrintAscend) 'Two threads are declared,
        Dim Descending As System.Threading.Thread = New Threading.Thread(AddressOf PrintDescend) 'ascending and descending 
        Sub Main()
            Console.WriteLine("VB.NET Final Project 12/30/14 . Option 3: Write a program to run two separate threads printing numbers or words (It can do both) in ascending and descending orders. The words have to be given by the user.")
            Ascending.Start() 'starts first thread
        End Sub
        Sub PrintAscend()
            Console.WriteLine("Please type in a word to print in ascending order.") 'the program asks for users to input a word
            Dim input As String 'as a string
            input = Console.ReadLine()
            For i As Integer = 0 To input.Length - 1
                Console.WriteLine(input.Chars(i)) 'prints the letters of the word in ascending order 
            Next
            If Ascending.ThreadState = Threading.ThreadState.Running Then Descending.Start() 'starts second thread
        End Sub
        Sub PrintDescend()
            Console.WriteLine("Please type in a word or number to print in descending order.")
            Dim input As String
            input = Console.ReadLine() 'user inputs the word that he wants
            For i As Integer = input.Length - 1 To 0 Step -1
                Console.WriteLine(input.Chars(i)) 'prints the letters of the word in descending order 
            Next
            Console.WriteLine("Please press enter key to close the program, and thanks for your time.")
            Console.ReadKey()
        End Sub


    End Module

    Wednesday, December 31, 2014 3:37 AM

Answers

All replies

  • I don't know what a predefined error means.

    I also do not know of any word in the English language which contains a numeric character.

    AFAIK the shortest words in the English language are a single letter and the longest word (disputable as a word for no apparent reason) is the word for the chemical name of Titin, which is the largest known protein, and is 189,819 letters in length.

    Perhaps you are supposed to catch errors if they occur. A word should not anything other than letters in it and I would guess could be between 1 and 189,919 characters in length. Words can have the same letter repeated in them also. So I guess capture any issues where a word entered doesn't follow some constraints.

    Since no dictionary is being used then there's really no way to verify if a word typed in is even a word.

    Titins chemical name. May need to zoom the browser in or download the image to read it all though.


    La vida loca


    Wednesday, December 31, 2014 5:13 AM
  • What do you mean by "predefined error" ?

    You can use Try / Catch to handle errors. Then you can show the predefined system errors with a msgbox that show the "Exception".

            Try
                'print 
                Dim PrintDocument1 As New System.Drawing.Printing.PrintDocument
                Dim PrintPreview1 As New PrintPreviewDialog
                AddHandler PrintDocument1.PrintPage, AddressOf OnPrintPage
    
                PrintPreview1.Document = PrintDocument1
                PrintPreview1.ShowDialog()
    
            Catch ex As Exception
                MsgBox("Printing Problem" & Chr(13) & ex.Message, MsgBoxStyle.Exclamation)
            End Try

    If you want to define your own then you look at the input and use select case.

            Do While theerror > 0
    
                theinput = Console.ReadLine()
    
                Select Case theinput
                    Case Is < 1
                        MsgBox("Input a number from 1 to 10)
                        theerror = 1
                    Case Is > 10
                        MsgBox("Input a number from 1 to 10)
                        theerror = 2
                    Case Else
                        theerror = 0
                End Select
            Loop
    
            PrintDescend()




    Wednesday, December 31, 2014 12:02 PM
  • PS Just in case the 400 viewers of this post dont know, this is not working code. You will need to deal with what Monkey is hinting at as well as other things.
    Wednesday, December 31, 2014 12:26 PM
  • My question is how would I add a predefined error to this program I did? Thanks!

    A predefined error is a class that you define that inherits from the Exception class.  See:
    http://msdn.microsoft.com/en-us/library/87cdya3t(v=vs.110).aspx

    You raise the exception using Throw and a new instance of that exception.  See:
    http://msdn.microsoft.com/en-us/library/ty79csek.aspx

    You can then catch that specific exception.   See:
    http://msdn.microsoft.com/en-us/library/3tca6706(v=vs.110).aspx

    • Edited by Acamar Wednesday, December 31, 2014 8:34 PM sp
    • Marked as answer by Cameron Hale Friday, January 2, 2015 3:33 AM
    Wednesday, December 31, 2014 8:33 PM
  • I know that a user-defined exception is basically a custom exception. But, I want to create one that will tell the person to type something in when the program asks for the person to put a word or number in to print in ascending order. Thanks. 
    Friday, January 2, 2015 4:21 AM
  • I want to create one that will tell the person to type something in when the program asks for the person to put a word or number in to print in ascending order.

    Adapt the example from that site to your custom message:

    ' Throws a new exception. 
    Throw New System.Exception("Type something in when the program asks for you to put a word or number in to print in ascending order.")

    But perhaps you aren't talking about exceptions at all.

    Friday, January 2, 2015 7:25 AM