Besonderhede van voorbeeld: -8701767870377617737

Metadata

Author: WikiMatrix

Data

Greek[el]
Αυτό γίνεται σωστά ως εξής: begin # Κάνε κάτι rescue Exception # όχι απλά rescue -- αυτό πιάνει μόνο την StandardError, υποκλάση της Exception # Χειρισμός της εξαίρεσης end Ο χειρισμός συγκεκριμένων εξαιρέσεων γίνεται ως εξής: begin # ... rescue RuntimeError # χειρισμός end Επίσης το αντικείμενο της εξαίρεσης μπορεί να γίνει διαθέσιμο στο σώμα κώδικα του χειριστή της εξαίρεσης: begin # ... rescue RuntimeError => e # χειριστής που μπορεί να χρησιμοποιεί το e, όπως η "print e.to_s" end Εναλλακτικά η πιο πρόσφατη εξαίρεση αποθηκεύεται στη "μαγική" καθολική μεταβλητή $!.
English[en]
To catch all exceptions one must write: begin # do something rescue Exception # Exception handling code here. # Don't write only "rescue"; that only catches StandardError, a subclass of Exception. end Or catch particular exceptions: begin # do something rescue RuntimeError # handle only RuntimeError and its subclasses end It is also possible to specify that the exception object be made available to the handler clause: begin # do something rescue RuntimeError => e # handling, possibly involving e, such as "puts e.to_s" end Alternatively, the most recent exception is stored in the magic global $!.

History

Your action: