我正在努力更好地理解异常处理。我有一个自定义错误处理程序。当在没有邮件服务器存在的情况下运行以下函数时,它将正确地触发错误处理程序并返回“艰难幸运”。在错误处理程序中,我必须使用throw new ErrorException()才能激活try/catch。如果没有此语句,在错误发生后,程序将继续使用下一行代码(在本例中返回“反馈左”)。
能够继续使用下一行代码是很有用的,因为用户不会收到响亮的错误消息和警告。但是,我希望能够在选定的情况下使用Try/Catch。
如果用户看不到错误,但我可以使用try/catch,那么有什么更好的方法来处理错误呢?有什么方法可以抛出新的ErrorExcept
我有一个函数,它基本上使用vector返回一个使用at(size_type pos)的元素。at()在无效(out_of_range)位置时抛出一个std::out_of_range异常。
我基本上希望这个异常被传播到调用者,这样就可以在那个级别上处理它。我添加到我的球衣中的重投是否有必要?或者,只要完全省略try-catch,我就能得到同样的效果吗?
int MyClass::GetNumber(size_t a_Idx) const
{
// Is the following try-catch-rethrow necessary? Or can the whole try-cat
Java中的异常处理有问题,下面是我的代码。当我试图运行这一行:throw new MojException("Bledne dane");时,出现了编译器错误。错误是:
异常MojException从不在相应的try语句体中抛出。
以下是代码:
public class Test {
public static void main(String[] args) throws MojException {
// TODO Auto-generated method stub
for(int i=1;i<args.length;i++){
为什么从类A的构造函数中抛出的以下异常会被捕获两次,第一次被构造函数本身的catch捕获,第二次被catch在主函数中捕获?
为什么它不被构造函数中的捕获捕获一次呢?
#include <iostream>
using namespace std;
class E {
public:
const char* error;
E(const char* arg) : error(arg) { }
};
class A {
public:
int i;
我的服务类中有方法。
@Transactional
public void serviceMethod {
dao.daoMethod();
}
public void daoMethod() {//dao.daoMethod
//some code
try {
//some more code that throws an unchecked exception
} catch(Exception exception) {
//do something -- no exceptions generated/thrown from
我有兴趣了解更多关于以下代码逻辑的行为:
try
{
// might, or might not do this: throw ExceptionTypeA;
function_which_might_throw_exception_type_a();
do_A(); // do we do A?
}
catch(ExceptionTypeB)
{
// B will never be done
do_not_do_B();
}
// C is always done (Edit: should always be done, but ac
我无法找到可靠的消息来源,以确保我能够在__destruct()中抛出异常。
php文档就是这样写的。
试图从析构函数抛出异常(在脚本终止时调用)将导致致命错误。
但当我测试的时候
class A
{
public function __destruct()
{
throw new \Exception();
}
}
try {
$a = new A();
} catch(\Exception $x) {
var_dump($x);
}
这似乎不是真的。我通常抛出和捕获异常。那么,为什么医生有这条线?
编辑后马克贝克的回答:
我有一个有异常的类,如果异常没有在类外部被捕获,我想要一个默认的异常。
下面是一些代码:
<?php
Class Test
{
public $file;
public function setFile($file)
{
$this->file = $file;
}
public function getFile()
{
return $this->file;
}
public function test()
{
try
{
我正在尝试理解在以下场景中处理异常的最佳方法是什么:
我有一个班级雇员:
class employee extends person {
private $salary;
private $baseSalary = 6.5;
function __construct($f, $m, $l, $a,$fsalary=0){
if(!is_numeric($fsalary)){
throw new Exception("Age supplied is not a number", 114);
}