Php 获取致命错误:无法使用try with catch或finally
当我尝试将php脚本上传到本地主机时,第60行出现了“致命错误:无法使用try而不使用catch或finally”。我不知道这是为什么,也不知道如何解决。此类的目的是在提交表单时验证潜在错误Php 获取致命错误:无法使用try with catch或finally,php,Php,当我尝试将php脚本上传到本地主机时,第60行出现了“致命错误:无法使用try而不使用catch或finally”。我不知道这是为什么,也不知道如何解决。此类的目的是在提交表单时验证潜在错误 <?php include("header.php"); require_once('./model/contactinfo.php'); require_once('./DAO/contactDAO.php'); ?> <div id="content"
<?php
include("header.php");
require_once('./model/contactinfo.php');
require_once('./DAO/contactDAO.php');
?>
<div id="content" class="clearfix">
<aside>
<h2>Mailing Address</h2>
<h3>1385 Woodroffe Ave<br>
Ottawa, ON K4C1A4</h3>
<h2>Phone Number</h2>
<h3>(613)727-4723</h3>
<h2>Fax Number</h2>
<h3>(613)555-1212</h3>
<h2>Email Address</h2>
<h3>info@wpeatery.com</h3>
</aside>
<div class="main">
<h1>Sign up for our newsletter</h1>
<p>Please fill out the following form to be kept up to date with news, specials, and promotions from the WP eatery!</p>
<?php
try{
$contactDAO = new contactDAO();
$hasError = false;
$errorMessages = Array();
if(isset($_POST['phoneNumber']) ||
isset($_POST['customerName']) ||
isset($_POST['emailAddress'])){
if(!is_numeric($_POST['phoneNumber']) || $_POST['phoneNumber'] == ""){
$hasError = true;
$errorMessages['phoneNumberError'] = 'Please enter a numeric phone number.';
}
if($_POST['customerName'] == ""){
$errorMessages['customerNameError'] = "Please enter a first name.";
$hasError = true;
}
if($_POST['emailAddress'] == ""){
$errorMessages['emailAddressError'] = "Please enter an email address.";
$hasError = true;
}
if(!$hasError){
$contact = new Contact($_POST['phoneNumber'], $_POST['customerName'], $_POST['emailAddress']);
$addSuccess = $contactDAO->addcontact($contact);
echo '<h3>' . $addSuccess . '</h3>';
}
}
}
if(isset($_GET['deleted'])){
**if($_GET['deleted'] == true){** This is where the error occurs
echo '<h3>Customer Deleted</h3>';
}
}
?>
<form name="fromcontact" id="fromcontact" method="post" action="contact.php">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="phoneNumber" id="phoneNumber" size='40'>
<?php if(isset($errorMessages['firstNameError'])){
echo '<span style=\'color:red\'>' . $errorMessages['firstNameError'] . '</span>';
}
?></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="phoneNumber" id="phoneNumber" size='40'>
<?php if(isset($errorMessages['phoneNumberError'])){
echo '<span style=\'color:red\'>' . $errorMessages['phoneNumberError'] . '</span>';
}
?></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" name="emailAddress" id="emailAddress" size='40'>
<?php if(isset($errorMessages['emailAddressError'])){
echo '<span style=\'color:red\'>' . $errorMessages['emailAddressError'] . '</span>';
}
?>
</tr>
<tr>
<td>How did you hear<br> about us?</td>
<td>Newspaper<input type="radio" name="referral" id="referralNewspaper" value="newspaper">
Radio<input type="radio" name='referral' id='referralRadio' value='radio'>
TV<input type='radio' name='referral' id='referralTV' value='TV'>
Other<input type='radio' name='referral' id='referralOther' value='other'>
</tr>
<tr>
<td colspan='2'><input type='submit' name='btnSubmit' id='btnSubmit' value='Sign up!'> <input type='reset' name="btnReset" id="btnReset" value="Reset Form"></td>
</tr>
</table>
<?php
$contact = $contactDAO->getContact();
if($contact){
echo '<table border=\'1\'>';
echo '<tr><th>contact ID</th><th>Customer Name</th>';
foreach($contact as $contact){
echo '<tr>';
echo '<td><a href=\'edit_contact.php?contactId='. $contact->getcontactId() . '\'>' . $contact->getcontactId() . '</a></td>';
echo '<td>' . $contact->getcustomerName() . '</td>';
echo '</tr>';
}
}
}catch(Exception $e){
echo '<h3>Error on page.</h3>';
echo '<p>' . $e->getMessage() . '</p>';
}
?>
</form>
</div><!-- End Main -->
</div><!-- End Content -->
<?php include("footer.php"); ?>
';
回显“”$联系人->获取客户名称();
回声';
}
}
}捕获(例外$e){
回显“第页错误”;
回显“”。$e->getMessage()。”;
}
?>
您有一个试一下块:
try {
$contactDAO = new contactDAO();
终止于此处:
echo '<h3>' . $addSuccess . '</h3>';
}
}
}
echo“”$获得成功;
}
}
}
您需要捕获某些内容或运行最后一段代码,否则,try
就没有意义了
很可能您根本没有想到右大括号应该放在哪里,因为代码后面实际上有一个catch
子句,只是在try
被关闭之后
我建议格式化您的代码,使缩进更清晰,这应该会立即向您说明问题的原因。您的大括号可能没有正确匹配,但很难判断,尤其是缩进混乱时。他在代码后面还有一个catch
块,但它不是try
块的一部分。因此,他可能有一些额外的右大括号,太早终止了try
。请您帮助我>错误>致命错误:不能使用try而不使用catch,或者最后在第55行的C:\wamp\www\anusthana\andro.php中使用@paxdiablo@Rathinam当前位置如果你想问一个问题,就把它当作一个问题来问,而不是作为对我答案的评论。前者会被所有人看到,后者只会被我看到。在任何情况下,错误都是不言自明的,您的代码有一个try,但没有catch/except。@paxdiablo我的帐户工作不正常,无法发布新问题,这就是我作为注释发布的原因,@paxdiablo FYI->解决了问题-只需添加->catch(异常$e){echo$e->getMessage();}