RingDotNetfor Ring

Common mistakes

Every beginner meets the same few error messages. This lesson makes each mistake on purpose, shows the message that Ring or .NET gives, says what it means and shows the fix. Keep it open the first time a program refuses to run.

What you will learn#

Two kinds of messages#

The message starts withIt comes fromExample
Error (R..)Ring itself: the program called something that does not exist, or with the wrong number of valuesError (R11) : Error in class name, class not found: stopwatch
DotNet Error:.NET: the call reached .NET and .NET refused itDotNet Error: System.ArgumentOutOfRangeException: Index was out of range...

A message of Ring is about the shape of the call (a name, a count of values). A message of .NET is about the values and the state of the objects (an index, a missing file, a text that is not a number). After DotNet Error: comes the name of the .NET exception, and its documentation (the tutorial Reading the .NET documentation) tells more.

The program#

Each mistake is inside try ... catch ... done, so the program goes on and shows the next one. In your own program the same mistake stops the program and prints the same message with the line number.

samples/UsingDotNet/tutorials/ch13_common_errors.ring
# common mistakes - each one is made on purpose inside try/catch, and the message that Ring or .NET gives is printed, with the fix
# errors on purpose
load "dotnet.ring"

using {
	"System"
	"System.Text"
	"System.Collections.Generic"
}

func main

	# Every mistake is in a try ... catch ... done block, so that the program goes on and shows the next one. In your own program the same
	# mistake stops the program and prints the same message with the line number.
	oList = new StringList
	oList.Add("pear")
	oList.Add("apple")

	# 1. A class that was not imported: Stopwatch is in System.Diagnostics, and the program did not ask for that namespace.
	try
		oWatch = new Stopwatch
	catch
		show("1","a class that is not imported",cCatchError)
	done
	? "   fix: add the namespace to using { ... }   (the class reference lists the namespace of every class)"
	dnImport("System.Diagnostics")
	oWatch = new Stopwatch
	? '   after using { "System.Diagnostics" } (here dnImport) a Stopwatch exists: ' + classname(oWatch)

	# 2. new Class() with empty parentheses.
	try
		oText = new StringBuilder()
	catch
		show("2","new StringBuilder() with empty parentheses",cCatchError)
	done
	oText = new StringBuilder
	oText = new StringBuilder(NULL)
	? '   fix: write new StringBuilder, or new StringBuilder(NULL), or give the value: new StringBuilder("x")'

	# 3. An overloaded method with no argument.
	try
		oList.Sort()
	catch
		show("3","an overloaded method called with no argument",cCatchError)
	done
	oList.Sort(NULL)
	? "   fix: NULL means no arguments: oList.Sort(NULL) gives " + oList.Item(0) + " first"

	# 4. A name with a typing mistake: a method, and a property.
	try
		oList.Ad("fig")
	catch
		show("4","a method name with a typing mistake",cCatchError)
	done
	try
		? oList.Cuont
	catch
		show("4b","a property name with a typing mistake",cCatchError)
	done
	? "   fix: look the name up in the class reference; Ring does not care about upper and lower case (oList.count is "+ oList.count +", the same as Count)"

	# 5. A .NET exception.
	try
		? oList.Item(9)
	catch
		show("5","an index outside the list (an exception of .NET)",cCatchError)
	done
	? "   the message starts with DotNet Error and the name of the exception; the text after it is the explanation of .NET"

	# 6. A value of the wrong kind.
	try
		? new Math().Sqrt("abc")
	catch
		show("6","a text where a number is needed",cCatchError)
	done
	? "   fix: pass what the parameter is: Sqrt(16) is " + new Math().Sqrt(16) + ". Use the class reference to see the types."

	# 7. A .NET null.
	oVariable = new Environment().GetEnvironmentVariable("NO_SUCH_VARIABLE_FOR_THE_TUTORIAL")
	try
		? "7. the variable is " + oVariable
	catch
		show("7","a text joined with a .NET null (the call returned null, and the answer is an object)",cCatchError)
	done
	? "   fix: ask first: oVariable.isNull() is " + oVariable.isNull()

func show cNumber, cTitle, cMessage

	? cNumber + ". " + cTitle
	? "   message: " + cMessage
Output
1. a class that is not imported
   message: Error (R11) : Error in class name, class not found: stopwatch
   fix: add the namespace to using { ... }   (the class reference lists the namespace of every class)
   after using { "System.Diagnostics" } (here dnImport) a Stopwatch exists: stopwatch
2. new StringBuilder() with empty parentheses
   message: Error (R19) : Calling function with less number of parameters
   fix: write new StringBuilder, or new StringBuilder(NULL), or give the value: new StringBuilder("x")
3. an overloaded method called with no argument
   message: Error (R19) : Calling function with less number of parameters
   fix: NULL means no arguments: oList.Sort(NULL) gives apple first
4. a method name with a typing mistake
   message: Error (R14) : Calling Method without definition: ad
4b. a property name with a typing mistake
   message: Error (R12) : Error in property name, property not found: cuont
   fix: look the name up in the class reference; Ring does not care about upper and lower case (oList.count is 2, the same as Count)
5. an index outside the list (an exception of .NET)
   message: DotNet Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   the message starts with DotNet Error and the name of the exception; the text after it is the explanation of .NET
6. a text where a number is needed
   message: DotNet Error: System.MissingMethodException: No public method System.Math.Sqrt matches the 1 argument(s)
   fix: pass what the parameter is: Sqrt(16) is 4. Use the class reference to see the types.
7. a text joined with a .NET null (the call returned null, and the answer is an object)
   message: Error (R50) : Object does not support operator overloading
   fix: ask first: oVariable.isNull() is 1

The mistakes, one by one#

1. Class not found (R11)#

class not found: stopwatch means that Ring has no class of that name. Either the name has a typing mistake, or the namespace was not imported. Stopwatch is in System.Diagnostics, and the program listed only System, System.Text and System.Collections.Generic. Add the namespace to using { ... } at the top. The class reference shows the namespace of every class.

2. new Class() with empty parentheses (R19)#

Calling function with less number of parameters after new StringBuilder(): a wrapped class has one init that takes the first value of the constructor, and () passes nothing. Write new StringBuilder (no parentheses), new StringBuilder(NULL) or give the value, new StringBuilder("x"). For a constructor with several values use named values: dnNew("Class",[ :name = value ]).

3. An overloaded method with no argument (R19)#

Most methods have several versions (overloads). Ring wraps them in one method with one parameter, so oList.Sort() has too few values. Pass NULL to call the version without parameters: oList.Sort(NULL). A list of :name = value pairs calls the version with those parameter names. A method that has no overloads takes no value: oTimer.Start().

4. A name with a typing mistake (R14 and R12)#

Calling Method without definition: ad is a method that the class does not have, and property not found: cuont is a property. Ring does not mind upper and lower case, but it does mind every letter. Look the name up in the class reference. If the class is a .NET one, remember that the name in Ring is the name in .NET: a method that was Add in the documentation is Add here.

5. A .NET exception#

DotNet Error: System.ArgumentOutOfRangeException is .NET telling that Item(9) is outside a list of two items. Other exceptions of the same family: FileNotFoundException (a missing file), FormatException (a text that is not a number), NullReferenceException (a call on a null). Check the condition before the call (oList.Count, File.Exists), or catch the error when the condition is not under your control (the tutorial When things go wrong).

6. A value of the wrong kind#

No public method System.Math.Sqrt matches the 1 argument(s) means that .NET has a method of that name, but none that accepts these values: a text "abc" where Sqrt needs a number. Pass a value of the kind that the parameter has. The class reference lists the types of the parameters.

7. A .NET null (R50)#

GetEnvironmentVariable returns null when the variable does not exist. The answer is an object, and "text" + oVariable is an operator that an object does not have (Object does not support operator overloading). Ask first: dnIsNull(oVariable) is true for a null (and false for every other value; oVariable.isNull() does the same for an object). Use it every time a method can return nothing (Environment, a dictionary lookup, a search that may not find).

How to read a message#

  1. Read the first words: Error (R..) is the shape of the call, DotNet Error: is the values.
  2. Read the name at the end of an R message (stopwatch, ad, cuont): it is the word that Ring did not know.
  3. Read the line that Ring prints under the message: the first line is where the call is.
  4. For a DotNet Error: read the exception name and the explanation, then look at the values that the call had.

Check the list#

MessageLikely causeFix
Error (R11) ... class not foundtyping mistake, or namespace not importedthe name, or using { "Namespace" }
Error (R19) ... less number of parametersnew Class(), or an overloaded method with no valuenew Class, or NULL
Error (R20) ... extra number of parameterstoo many values for an overloaded method, which takes oneone list of named values [ :a = 1, :b = 2 ], or callMethod("Name",[a, b]) for values in order
Error (R14) ... Method without definitiona method name that the class does not have, or an object that has no classlook up the name; callMethod for an object with no class
Error (R12) ... property not founda property name that the class does not havelook up the name
Error (R50) ... operator overloadingan object used as a text or a number (often a null)dnIsNull(value), or read the value you need
DotNet Error: ...MissingMethodExceptionvalues of the wrong kind or countmatch the types of the parameters
DotNet Error: ... another exceptionthe state or the valuesread the exception name and check the values
Tip

When a call fails and the cause is not clear, make it smaller: call the same method in a short program with the plainest values. If that works, the difference between the two programs is the cause.

What you learned#

Exercises#

  1. Change a name in the program of an earlier lesson and read the message. Which of the seven is it?
  2. Write a function safeItem(oList, nIndex) that returns "" when the index is outside the list, using try ... catch, and another that checks Count first. Which one is shorter?
  3. Find a .NET method that can return null (hint: ask Environment for a variable) and print "missing" when it does.
  4. Call Math.Max with one value, with a text and with two numbers. Which messages do you get?

Next#

More than one system

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.