php怎么捕获异常?
php怎么捕获异常?
有没有类似.net try catch机制?
php不能逐行调试 蛋疼!
回复讨论(解决方案)
有的 写法一样
try {
} catch (Exception $e) {
print $e->getMessage();
}
有没有类似.net try catch机制? 这个真心有~
php不能逐行调试 蛋疼! 楼主没找对ide
其实php的调试可以随时调,想在哪里调,最简单就是在哪里输出。当然zend-studio也提供了断点功能调试,但是远不如这样方便。
其实都各有各的好处,哥,还是从了吧。
#1 可爱北极熊 正解
try{ $a="sgfdfgdf"; $b= (int)$a; echo($b);}catch(exception $e){ echo("出错啦".$e->getMessage());//这里并木有执行啊}
还需要在try{}语句块里抛出异常 throw new Exception('It is an exception!') ,然后由catch(Exception $e) 捕获。
还需要在try{}语句块里抛出异常 throw new Exception('It is an exception!') ,然后由catch(Exception $e) 捕获。 \
这样虽然能捕获到 但错误信息还要我指定啊
我要的就是错误信息啊 我都知道了还捕获个鬼啊
用echo来放点找异常,php没法啊
那就去看php的error_log啊...会有各种详细的报错信息....还有啥可catch的...
那就去看php的error_log啊...会有各种详细的报错信息....还有啥可catch的...
php的error_log在那里看?
错误大体有两种,一种是php可以捕获到的,例如语法错误。还有一种,是逻辑上的错误,这种错误机器并不能捕获,所以才会用到try catch人为捕获。
我想你说的错误只是前一种,那么打开错误提示display_errors = On,就可以看到。
还有set_exception_handle。
你这段代码没异常,当然不会执行那句
PHP code
try{
$a="sgfdfgdf";
$b= (int)$a;
echo($b);
}
catch(exception $e){
echo("出错啦".$e->getMessage());//这里并木有执行啊
}
你这段代码没异常,当然不会执行那句
引用 5 楼 telankes2000 的回复:
PHP code
try{
$a="sgfdfgdf";
$b= (int)$a;
echo($b);
}
catch(exception $e){
echo("出错啦".$e->getMessage());//这里并木有执行啊
}
整形转换错误 这还不是错误啊
这种错误系统应该是自动捕获的
咳咳,这个不是错,在php里, 自动转换
引用 14 楼 helloyou0 的回复:
你这段代码没异常,当然不会执行那句
引用 5 楼 telankes2000 的回复:
PHP code
try{
$a="sgfdfgdf";
$b= (int)$a;
echo($b);
}
catch(exception $e){
echo("出错啦".$e->getMessage());//这里并木有执行啊
……
整形转换错误 这还不是错误啊 在任何语言中,强制类型转换都不会是错误!
虽然有时你并没有得到期望的结果
php的错误异常和java的类似,不过一般都要自己去抛出:例子:
try{
$m = 0;
if($m throw new Exception('m='.$m.':小于零');
}
echo $m;
} catch(exception $e){
echo "错误[line->".($e->getLine())."]: ".($e->getMessage());
}
php弱类型 改变数据类型没有问题
让除数为0 ,就会看到抛异常了
我也有相同的疑问。
例如我在php里面用到了$_GET,但是其实并没有传入任何get参数,这个时候就会出错,我用try catch并不能捕捉到这个错误。
请问如何才能知道get出错然后进行相应的处理呢

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.
