User Tools

Site Tools


Try - Catch

The Try - Catch block is very important, and should be used where an error can occur, for example if a file that needs to be read does not exist, you are specifying an invalid path, the current user has not the rights to read and so forth - please read the documentation about System.IO.File.ReadAllText() to see what type of exceptions there are.

In classic VO and Win32 applications, you had to check the return value of an operation, and then eventually ask for more details.

The .NET Framework works differently: it produces an exception, and it is the programmers responsability to treat it correctly.

A sample:

using System.IO
function TestException() as void
local cBuffer as string
cBuffer := File.ReadAllText( "c:\temp\This is an  invalid filename : txt" )
catch oEx as ArgumentException
System.Console.WriteLine( "invalid argument:" + oEx:Message + " in argument " + oEx:ParamName )
catch oEx as NotSupportedException
System.Console.WriteLine( "invalid path: " + oEx:Message )
catch oEx as Exception
System.Console.WriteLine( "Error reading the file: " + oEx:Message )
end try

If you don't use the Try - Catch block, your program is terminated with a unspecified error, and you will not be able to understand what really occurred.

Please see also here: Exceptions

trycatch.txt · Last modified: 2018/08/20 06:05 by wolfgangriedmann