Top

Error

run-time error

when you run the code, some non-spelling errors occur, for example:

divide by zero error

1/0 
// output: Division by ZERO 

array bounds

arr = [1]
arr[8] // try to read the element with index 8, but the length is actually only 1 
// output: reflect: slice index out of range 

Go native error

is calling Go standard library, third party When the library is used, there will be many methods that return the value as [Result …, error message], for example

// Go Standard Library-String conversion integer method 
strconv.Atoi("NaN") 

in Go , The signature of the above method is

package strconv 
... 
func Atoi (s string) (int, error) ( 
... 
) 

The return value contains int and error types, and the result returned by Z1h is an array :

res = strconv.Atoi (" NaN ")
// Output: [0, {"Func": "Atoi", "Num": "NaN", "Err": {}}] 

At this time, you can check whether the [1] th parameter is empty Check if an error is thrown, if it is empty, then [0] is the desired result

res = strconv.Atoi (" NaN ") 
if (res [1]) { 
print ('An error has occurred:', res [1] .Error ()) 
} else { 
print ('got the answer:', res [0]) 
} 
  • You can try to replace" NaN "with" 123 "in the above example *

Throw An error

occurs. When the code runs somewhere, you need to forcibly interrupt the subsequent operations and pass the error information up, you need to throw an exception

-call the panic function to throw an error -when you call the assert method, the parameters passed in If there is an error, the error will also be thrown.

Example of

panic usage

panic ("Bye") 
print ("will not be executed here") 
// Output: Panic at call func:Bye

The use of the assert

print(assert(strconv.Atoi("123")))
print(assert(strconv.Atoi("NaN")))
print ("Here will not be executed") 
// The first line will output 123 
// The second line will throw an error 
// The third line will be interrupted by the second paragraph, will not be executed 

Error In the

Z1h language, you can use the try {...} catch (e) {...} code block to uniformly catch the thrown exception

as follows, catching the exception of the string conversion integer and processing

var str = "Not a number"; // Can be replaced with "888" Try 
{ 
var num = assert (strconv.Atoi (str)); 
print ("Get the number:", num); 
} catch (e) { 
print (`String $ {str} is not a valid integer: $ {e.Error ()}`); 
}