我在Clojure中有一个函数,我想在发生异常时返回true,就像下面的代码一样;
(try
(code)
(catch Exception e (= 1 1)))
然而,上述方法给出了以下错误:
ClassCastException java.lang.Boolean不能强制转换为clojure.lang.IFn signal.message / read? (message.clj:12)
只有在放置函数时才有效:
angular.module('starter.controllers', [])
.controller('SearchCtrl', function ($scope, $http, $ionicLoading) {
$scope.results = [];
$scope.search = function () {
$ionicLoading.show({ template: 'Loading...' });
$http.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + $scope.searchtext + '&key=').
then(function (data) {
var searchResults = data
angular.forEach(searchResults.data.items, function (val, i) {
$http.get('https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=' + val.id.videoId + '&key=').
then(function (data) {
var durationResults = data
var duration = durationResults.data.items[0].contentDetails.duration.replace("PT", "").replace("H", ":").replace("M", ":").replace("S", "")
$scope.results.push({ thumbnail: val.snippet.thumbnails.medium.url, title: val.snippet.title, channel: durationResults.data.items[0].snippet.channelTitle, description: val.snippet.description, duration: duration })
});
});
});
$ionicLoading.hide();
};
})
<ion-view title="Search for Music">
<ion-content scroll="true" class="has-header padding">
<form ng-submit="search()" class="list list-inset">
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" name="searchtext" ng-model="searchtext" placeholder="Search">
</label>
</form>
<ion-list>
<ion-item ui-sref="home" ng-repeat="result in results" class="item item-thumbnail-left">
<img src="{{result.thumbnail}}">
<h2>{{result.title}}</h2>
<strong>{{result.channel}}</strong>
<p>{{result.description}}</p>
<p>Duration: {{result.duration}}</p>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
任何方式返回Clojure catch的值?
答案 0 :(得分:5)
使用:
测试catch
(try
(throw (RuntimeException.))
(catch Exception e true)))
运行此代码将返回true
。
答案 1 :(得分:1)
你在问题中所写的内容是完全正确的:如果(code)
抛出异常,那么try / catch as write会返回true。问题出在其他地方:这个try / catch周围的代码有太多括号,或者你粘贴的代码与你正在运行的实际代码不匹配。