Skip to content

Files

Latest commit

 

History

History
102 lines (85 loc) · 3.25 KB

README-FlowControl-Exceptions.md

File metadata and controls

102 lines (85 loc) · 3.25 KB

LOGO

1. What are exceptions

MinitScript supports exceptions, which are categorized as flow control.

Exceptions are about 3 things. Trying to do some work, that can issue a exception, which means a failure in logic, and catching the exception. Third is also how to throw exceptions.

Exceptions do not need to be catched at the level of the throw statement, it can also be catched a level higher from a calling function.

1.1. Trying and catching

module

function: module1Init()
	console.printLine("-----------------")
	console.printLine("init")
	console.printLine("-----------------")
	# initialize our array
	$array = []
	try
		# reading data.txt into an array
		$array = filesystem.getContentAsStringArray(".", "data.txt")	
		# filesystem.getContentAsStringArray() would throw an exception if data.txt does not exist
		# in this case the following functions would not be executed anymore
		# the script would jump into catch block
		# and we would not see this message
		console.printLine("data.txt: read file into memory");
	catch ($exception)
		console.printLine("An error occurred: " + $exception)
		# print the stack trace
		console.printLine(stackTrace())
	end
end

1.2. Throwing an exception

module;

function: module1Init()
	# this throws an exception with the string argument "not implemented"
	# the exception can be catched at a higher level
	throw("not implemented")
end

1.3. Initialization example

use: module_1.tscript
use: module_2.tscript
use: module_3.tscript

# main
function: main()
	console.printLine("---------")
	console.printLine("Nothing")
	console.printLine("---------")
	console.printLine()
	# initialize modules, which can load data from files or databases, or computing some values, ...
	try
		# if any of the module initializations throw an unhandled exception, the exception will be handled here in catch block. then the script will stop.
		module1Init()
		module2Init()
		module3Init()
	catch ($exception)
		# ahhhhh! one initialization failed!
		console.printLine("An error occurred: " + $exception)
		# print the stack trace
		console.printLine(stackTrace())
	end
end

2. Links

2.1. Language documentation

2.2. Other links