मैं एक अपवाद फेंक करना चाहते हैं और निम्नलिखित है:क्लोजर में मैं अपवाद कैसे फेंक सकता हूं?
(throw "Some text")
हालांकि इसे अनदेखा किया जा रहा है।
मैं एक अपवाद फेंक करना चाहते हैं और निम्नलिखित है:क्लोजर में मैं अपवाद कैसे फेंक सकता हूं?
(throw "Some text")
हालांकि इसे अनदेखा किया जा रहा है।
आप एक Throwable में अपने स्ट्रिंग रैप करने के लिए की जरूरत है:
(throw (Throwable. "Some text"))
या
(throw (Exception. "Some text"))
आप आज़माएं/कैच/अंत में के रूप में अच्छी तरह से ब्लॉक की स्थापना कर सकते हैं:
(defn myDivision [x y]
(try
(/ x y)
(catch ArithmeticException e
(println "Exception message: " (.getMessage e)))
(finally
(println "Done."))))
आरईपीएल सत्र:
user=> (myDivision 4 2)
Done.
2
user=> (myDivision 4 0)
Exception message: Divide by zero
Done.
nil
शानदार उत्तर। धन्यवाद! – Zubair
clojure.contrib.condition अपवादों को संभालने का क्लोजर-फ्रेंडली माध्यम प्रदान करता है। आप कारणों के साथ conditons उठा सकते हैं। प्रत्येक स्थिति में अपना खुद का हैंडलर हो सकता है।
source on github में कई उदाहरण हैं।
यह काफी लचीला है, जिसमें आप अपनी कुंजी, मूल्य जोड़े जोड़ते समय प्रदान कर सकते हैं और फिर तय कर सकते हैं कि कुंजी/मूल्यों के आधार पर आपके हैंडलर में क्या करना है।
उदा। (उदाहरण के कोड mangling):
(if (something-wrong x)
(raise :type :something-is-wrong :arg 'x :value x))
फिर आप :something-is-wrong
के लिए कोई हैंडलर हो सकता है:
(handler-case :type
(do-non-error-condition-stuff)
(handle :something-is-wrong
(print-stack-trace *condition*)))
अब इसे [slingshot] (https://github.com/scgilardi/slingshot) के साथ प्रतिस्थापित किया गया है ([Clojure.Contrib Go कहाँ से लिया गया था] (http: //dev.clojure।org/प्रदर्शन/डिजाइन/कहाँ + क्या + Clojure.Contrib + जाओ)) – kolen
आप एक अपवाद फेंक करना चाहते हैं और (इसमें कुछ डिबगिंग जानकारी शामिल संदेश के अलावा हैं स्ट्रिंग), आप अंतर्निहित ex-info फ़ंक्शन का उपयोग कर सकते हैं।
पहले से निर्मित पूर्व-सूचना ऑब्जेक्ट से डेटा निकालने के लिए, ex-data का उपयोग करें। clojuredocs से
उदाहरण:
(try
(throw
(ex-info "The ice cream has melted!"
{:causes #{:fridge-door-open :dangerously-high-temperature}
:current-temperature {:value 25 :unit :celcius}}))
(catch Exception e (ex-data e))
एक टिप्पणी Kolen उल्लेख slingshot, जो उन्नत कार्यक्षमता है कि आप न केवल (फेंक +) के साथ मनमाना प्रकार की वस्तुओं फेंक करने की अनुमति देता प्रदान करता है में, लेकिन यह भी एक और अधिक लचीला पकड़ सिंटैक्स का उपयोग करें फेंक दिया वस्तुओं के अंदर डेटा का निरीक्षण करने के लिए (कोशिश + के साथ)। the project repo से उदाहरण:
टेन्सर/parse.clj
(ns tensor.parse
(:use [slingshot.slingshot :only [throw+]]))
(defn parse-tree [tree hint]
(if (bad-tree? tree)
(throw+ {:type ::bad-tree :tree tree :hint hint})
(parse-good-tree tree hint)))
गणित/expression.clj
(ns math.expression
(:require [tensor.parse]
[clojure.tools.logging :as log])
(:use [slingshot.slingshot :only [throw+ try+]]))
(defn read-file [file]
(try+
[...]
(tensor.parse/parse-tree tree)
[...]
(catch [:type :tensor.parse/bad-tree] {:keys [tree hint]}
(log/error "failed to parse tensor" tree "with hint" hint)
(throw+))
(catch Object _
(log/error (:throwable &throw-context) "unexpected error")
(throw+))))
'throw' जावा' Throwable' के उदाहरण फेंकता है। क्या '(फेंकना (अपवाद।" कुछ पाठ ")) 'काम? – dfan
जब मैं कोशिश करता हूं ("कुछ पाठ" फेंक दो) मुझे कक्षा क्लास एक्सेप्शन मिलता है क्योंकि स्ट्रिंग को फेंकने योग्य नहीं किया जा सकता है। तो यह अजीब बात है कि फेंक को आपके मामले में "अनदेखा" किया जा रहा है .... – mikera