Exceptions and execution are always associated. If you open a file that does not exist and do not handle the situation properly, your program is considered to be of low quality.
If an exception occurs, the program stops. Exceptions are used to handle various types of errors that may occur during program execution, so take appropriate action without causing the program to stop completely.
Ruby provides a perfect mechanism for handling exceptions. We can do it in From For If the exception does not match all the specified error types, we can use all the The running output of the above example is. As you can see, You can use the The following is the process flow: An exception occurred when it was opened. Skip to Pass through The file was opened successfully this time. Continue the basic process. Note: if the renamed file does not exist, this example code will try indefinitely. So when handling exceptions, use them cautiously You can use the The first form simply re-throws the current exception (throwing a RuntimeError if there is no current exception). This is used in exception handlers that need to interpret the exception before passing in the exception. The second form creates a new The third form uses the first parameter to create an exception, and then sets the related message to the second parameter. The fourth form is similar to the third form, where you can add any additional conditional statements, such as The output of the above instance is as follows: Another demonstration. The output of the above instance is as follows: Sometimes, whether or not an exception is thrown, you need to make sure thatsome processing is done at the end of the code block. For example, you may open a file when you enter, and when you exit the block, you need to make sure that the file is closed. This is what the The output of the above instance is as follows: If provided The body of the The output of the above instance is as follows: Use In the following example, if the user types The above program requires human interaction, and you can try it on your computer. The output of the above instance is as follows: Ruby’s standard classes and modules throw exceptions. All exception classes form a hierarchy, including the top Let’s look at an example: Now, looking at the following example, you will use the above exception: The most important line here is raise
最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。 begin/end block with code that may throw an exception, and use the rescue clause tells Ruby the perfect type of exception to handle. 6.30.1. Grammar #
Begin # Start raising# Throw an exception, execute [ExceptionType=Standard Exception] # The default value for capturing exceptions of the specified type is StandardException $# Indicates exception information $@ # indicates the code location where the exception occurred else # Other exceptions Ensure #Enter the code block end # regardless of any exceptions
begin to rescue Everything in is protected. If an exception occurs during the execution of the code block, control is passed to the rescue and end between the blocks. begin Each in the block rescue clause, Ruby takes turns comparing the thrown exception with each parameter. If rescue the exception named in the clause is the same as the type of exception currentlythrown, or is the parent of the exception, the match is successful. rescue clause with a else clause. 6.30.2. Example #
#!/usr/bin/rubybeginfile=open("/unexistant_file")iffileputs"File opened successfully"endrescuefile=STDINendprintfile,"==",STDIN,"\\n"
STDIN replaced file because it failed to open# Use retry statement #
rescue block to catch an exception, and then use the retry the statement is executed from the beginning begin block. 6.30.3. Grammar #
Begin # The exception thrown by this code will be resolved by the following Clause capture rescue # This block will capture all types of exception retries #This will move control to begin The beginning end of
6.30.4. Example #
#!/usr/bin/rubybeginfile=open("/unexistant_file")iffileputs"File opened successfully"endrescuefname="existant_file"retryend
rescue . fname is reassigned. retry skip to begin the beginning. retry . Use
raise statement # raise statement throws an exception. The following method throws an exception when called. Its second message will be output. 6.30.5. Grammar #
raise or raise"Error Message" or raiseExceptionType,"Error Message" or raiseExceptionType,"Error Message"condition
RuntimeError exception, set its message to the given string. The exception is then thrown to the call stack. unless to throw an exception 6.30.6. Example #
#!/usr/bin/rubybeginputs'I am before the raise.'raise'An error has occurred.'puts'I am after the raise.'rescueputs'I am rescued.'endputs'I am after the begin block.'
I am before the raise. I am rescued. I am after the begin block.
raise examples of usage: 6.30.7. Example #
#!/usr/bin/rubybeginraise'A test exception.'rescueException=>eputse.messageputse.backtrace.inspectend
A test exception. ["main.rb:4"]
Use
ensure statement # ensure clause does. ensure put it on the last one``rescue`` clause and contains a block of code that is always executed when the block terminates. It has nothing to do with whether the block exitsnormally, whether an exception is thrown and handled, or whether it is terminated because of an uncaught exception. ensure block always runs. 6.30.8. Grammar #
Begin # Process # Throw exception rescue # Processing error ensure #Finally, ensure the execution of # This always executes end
6.30.9. Example #
beginraise'A test exception.'rescueException=>eputse.messageputse.backtrace.inspectensureputs"Ensuring execution"end
A test exception. ["main.rb:4"] Ensuring execution
Use
else statement # else clause, which is usually placed in the rescue after the clause, any ensure before. else clause executes only if the body of the code does not throw an exception. 6.30.10. Grammar #
Begin # Process # Throw exception rescue # Processing error else # If there are no exceptions, execute ensure # Finally, ensure the execution of # This always executes end
6.30.11. Example #
begin#Throw 'A test exception.'puts"I'm not raising exception"rescueException=>eputse.messageputse.backtrace.inspectelseputs"Congratulations-- no errors!"ensureputs"Ensuring execution"end
I'm not raising exception Congratulations-- no errors! Ensuring execution
$! variables can catch thrown error messages. Catch and Throw #
raise and rescue exception mechanism can abort execution when an error occurs, and sometimes need to jump out of some deeply nested structureduring normal processing. At this time catch and throw it comes in handy. catch defines a block that uses a given name (which can be Symbol or String) as a label. Block will execute normally until one is encountered. throw . 6.30.12. Grammar #
Throw: lablename # This will not be executed by catch: lablenamedo # When encountering a throw The catchend or throw that will be executed after matching: lablenamecondition # This will not be executed by catch: lablenamedo # Matching will be executed after encountering a throw Catchend
Example #
'!' in response to any prompts, use a throw terminates interaction with the user. 6.30.13. Example #
defpromptAndGet(prompt)printpromptres=readline.chompthrow:quitRequestedifres=="!" returnresendcatch:quitRequesteddoname=promptAndGet("Name:")age=promptAndGet("Age:") sex=promptAndGet("Sex:")#..#process information endpromptAndGet("Name:")
Name: Ruby on Rails Age: 3 Sex: ! Name:Just Ruby
Class
Exception # Exception including the class. The next layer is seven different types:
Interrupt NoMemoryError SignalException ScriptError StandardError SystemExit Fatal is another exception in this layer, but the Ruby interpreter uses it only internally. ScriptError and StandardError there are some subclasses, but we don’t need to know these details here. The most important thing is to createour own exception classes, which must be classes Exception or a subclass of its descendants. 6.30.14. Example #
classFileSaveError<StandardErrorattr_reader:reasondefinitialize(reason)@reason=reasonendend
Example #
File.open(path,"w")do\|file\|begin# Write data ...rescue# An error occurred raiseFileSaveError.new($!)endend
FileSaveError.new($!) . We call raise to indicate that the exception has occurred and pass it on to FileSaveError fails to write data due to a specific exception