我正在使用我的一个静态方法处理异常处理。我发现了MissingMethodException(通过试验和错误)但是当我编写代码来捕获它时,Grails 2.3.11忽略了catch块。甚至没有使用默认的Exception。为什么没有抓住异常?
package utility
import java.text.SimpleDateFormat
class DateUtility {
static String getGrailsDefaultDate(String datetm) {
def format = new SimpleDateFormat("YYYYMMddHHmmss")
try{
def date = format.parse(datetm)
date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(date)
datetm = date.toString()
}
catch(IllegalArgumentException iae){
datetm = "An error occured retrieving the date"
}
catch(NullPointerException npe){
datetm = "A date was not provided"
}
catch(java.text.ParseException pe){
datetm = "Unparseable date"
}
catch(groovy.lang.MissingMethodException mme){
datetm = "A missing method occured"
}
catch(Exception e){
datetm = "default exception"
}
return datetm
}
}
这是应该抛出缺少的方法异常的调用:
DateUtility.getGrailsDefaultDate(1)
答案 0 :(得分:1)
为什么没有抓住异常?
您的catch
块只会捕获从try
块引发的异常。您的try
块中没有任何表达式会抛出MissingMethodException
,因此不会调用捕获catch
的{{1}}块。这一切看起来都按设计工作。
如果您将MissingMethodException
置于DateUtility.getGrailsDefaultDate(1)
阻止try
阻止catch
阻止MissingMethodException
阻止catch
阻止class DateUtility {
static String getGrailsDefaultDate(String datetm) {
def format = new SimpleDateFormat("YYYYMMddHHmmss")
try{
def date = format.parse(datetm)
date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(date)
datetm = date.toString()
}
catch(IllegalArgumentException iae){
datetm = "An error occured retrieving the date"
}
catch(NullPointerException npe){
datetm = "A date was not provided"
}
catch(java.text.ParseException pe){
datetm = "Unparseable date"
}
catch(groovy.lang.MissingMethodException mme){
datetm = "A missing method occured"
}
catch(Exception e){
datetm = "default exception"
}
return datetm
}
static void main(args) {
try {
DateUtility.getGrailsDefaultDate(1)
} catch (MissingMethodException mme) {
println 'I Caught The Exception!'
}
}
}
阻止$c
阻止被调用。
试试这个......
<?php
foreach($Arows as $row){
$html = "";
$html .= '<tr class="row">'."\r\n";
$html .= '<th class="col-sm-12"><h4>'.$row['Type_A'].'</h4></th>'."\r\n";
$html .= '</tr>'."\r\n";
$stmt3 = $con->prepare("SELECT * FROM sous_analyse WHERE Id_A =".$row['Id_A']);
$stmt3->execute();
$SArows = $stmt3->fetchAll();
$c = 1;
foreach($SArows as $Srow){
$html .= '<tr class="row">'."\r\n";
$html .= '<td class="col-sm-4">'.$Srow['Parameter'].'</td>';
$html .= '<td class=" col-sm-5"><input type="text" class="form-control col-xl-12 border-secondary" name="Fonctionnalite" placeholder="RESULTATS" required=""></td>';
$html .= '<td class="col-sm-3 text-center">'.$Srow['Normal'].'</td>';
$html .= '</tr>';
}
$html .= '<tr class="row">';
$html .= '<th class="col-sm-12">';
$html .= '<div class="input-group">';
$html .= '<span class="input-group-addon form-check">';
$html .= '<span class="checkbox-custom checkbox-default">';
$html .= '<input type="checkbox" class="icheckbox-grey" id="inputUnchecked-$c" name="inputUnchecked">';
$html .= '<label for="inputUnchecked-$c"></label>';
$html .= '</span></span>';
$html .= '<textarea class="form-control" id="textareaDefault-$c" rows="3" style="margin-top: 0px; margin-bottom: 0px; height: 60px;" disabled></textarea>';
$html .= '</div></th></tr>';
echo $html;
$c++;
}
?>