Hi, I have a question. In a specific function, when my software failed to load a json file with from_json file, I need to exit the current function without stopping my software, how can I proceed ?
Is it possible to do that ?
Hi, I have a question. In a specific function, when my software failed to load a json file with from_json file, I need to exit the current function without stopping my software, how can I proceed ?
Is it possible to do that ?
Pretty sure you’d just use return
keyword?
def test
puts "start"
return
puts "end"
end
test # => start
My question was stupid, but I wasn’t sure it’s a good habit to use return like that, because I thought return must return a value
Not every method needs to return a value. Return types - Crystal is a good way to denote that the method’s return value doesn’t matter.
Does not have to have, but :
this sounds like it would be good to return something that tells the caller that the function encountered a problem and could not do it’s job.
Like return false
or invent some error type to that effect.
I will, my goal is to alert the user about the software failed to load one or some json files, but I can’t accept this error completely stop the software for some reasons, that’s why even I will print an alert, I just need to exit the current function , but not the entire software
Just one more question, how can I access to the JSON::ParseException object when an error occur with from_json method ? There isn’t an example in the documentation.
This is my code related to:
begin
information = Information.from_json(File.read(loadInformationFilePath))
rescue
puts "Syntax errors detected during file load process "+loadInformationFilePath+" at line number "#error.line_number
return
end
At the end of my puts instruction, I need to print the line number where the error is
See Exception handling - Crystal for how to get the Exception
instance. However there isn’t an easy way to get the line number at the moment. You’ll either have to manually parse the backtrace (include first line of it), or not include it at all.
Ref: Improve Exception backtrace API · Issue #10681 · crystal-lang/crystal · GitHub
But normally, the doc related to from_json function say if it failed, it raise this kind of exception:
https://crystal-lang.org/api/1.7.2/JSON/ParseException.html
And this exception can return the line number (it’s write )
No ?
Wait, by “line number” do you mean the number in the JSON document that was invalid causing the exception, or the line number in the program in which the exception was raised from?
These are two different meanings of “line number”. JSON::ParseException
’s line_number
property represents the former. The latter meaning is most of the time available, but isn’t as easy to access as you have to parse it from the trace of the exception, probably via a regex.
I think I got the point how to catch the JSON exception, I missed one explanation at the tutorial you gave me. Thanks.
About the line, I talk about the line in the JSON file, not in my code.
Okay, cool. In that case it would be something like:
begin
# ...
rescue ex : JSON::ParseException
ex.line_number
end