views:

44

answers:

1

Fatal error: Uncaught exception 'EppCommandsExceptions' with message 'Required parameter missing'

The line in question:

throw new EppCommandsExceptions($result->msg, $codigo); 

Why am I having this error on this line?

On EppCommandsExceptions.class.php I have this class that extends Exception:

class EppCommandsExceptions extends Exception
{
    //could be empty.
}

Next, on CommandsController.php I have:

include_once('EppCommandsExceptions.class.php');

and, later, if something bad happens on method1:

throw new EppCommandsExceptions($result->msg, $codigo);

later, on this same controller, another method2 that will run after method1, I have: if something goes bad with this too:

throw new EppCommandsExceptions($result->msg, $codigo);

Later I have, for the contact part - method1

try
{
    $createdContact = $comandos->createContact($contactoVo);
}
catch(EppCommandsExceptions $e)
{
    $error .= 'Error Contact. Cód:'.$e->getCode().' Mensagem:'.$e->getMessage();
}

And later, for domain part: method2

try
{
    $createdDomain = $comandos->createDomain($domainVo);
}
catch(EppCommandsExceptions $e)
{
    $error .= 'Error Domain. Cód:'.$e->getCode().' Mensagem:'.$e->getMessage();
}

Is it because I'm using the same exception for both methods? Should I have one Exception class for EACH method? :s

Please advice, Thanks a lot. MEM

A: 

The exception you throw will only be caught if it's inside a try block.

If it isn't it'll propagate up the call stack, until it is caught in one of the earlier calling functions.

You're getting that fatal error because the exception you throw is never caught, so it's handled by the default unhandled exceptions handler, which emits the fatal error.

Examples:

try
{
    $createdContact = $comandos->createContact($contactoVo);
    if (error_condition())
        throw new EppCommandsExceptions $e;
}
catch(EppCommandsExceptions $e)
{
    $error .= 'Error Contact. Cód:'.$e->getCode().' Mensagem:'.$e->getMessage();
}

Throwing the exception directly in the try block is not usually very useful, because you could just as well recover from the error condition directly instead of throwing an exception. This construct becomes more useful, though, if createContact may throw an exception. In this case, you have at some point to catch EppCommandsExceptions to avoid a fatal error.

Artefacto
I've not totally understand your answer. So this can be due to the fact that I've not get it all.So:"The exception you throw will only be caught if it's inside a try block."This exception will work, if I comment the line containing the throw on method2.The throw of method1 that works is NOT inside a try catch, but, inside a conditional (on the else part).Hence, this exception seems to work when called outside a try block.:s?
MEM
@MEM That just means the thrown exception in method2 is never caught while that thrown in method1 is, either in method1 itself or upstream.
Artefacto
Got me.I don't understand what you mean by "up the call stack" My ignorance.Np for now. Need to solve this:"Throwing the exception directly in the try block is not usually very useful, because you could just as well recover from the error condition directly instead of throwing an exception."Yes. And that's why I'm NOT doing it."This construct becomes more useful, though, if createContact may throw an exception."But createContact is throwing an exception."In this case, you have at some point to catch EppCommandsExceptions to avoid a fatal error."Ok. And I'm catching that on method 2.
MEM
Are you telling me to put my throw on createContact inside a try catch statement?
MEM
@MEM Certainly not. Look, when I get an unhandled exception, PHP shows you the whole call stack, including the affected files and lines. The only way you're getting this is if each one of the lines indicated in the call stack are outside a try block that catches your type of exception. Check those.
Artefacto
Oh... so call stack is like a list of subsequent calls that had being made... :s ok.So I need to see if any of those line are outside a try block, and if they are, then that's the reason for having this error. I will have a look.
MEM
@MEM You want to check if they're **all** outside a try/catch block that catches this particular exception. That's the only way you get the fatal error.
Artefacto
I've see them. I was having a throw of createContact, but createContact was NOT inside a try catch block. I've putting it there, I no longer have that error. Artefacto. Thank you, for you patience. :)All fine now. :D
MEM
So, we were throwing some exception but we were not not catching it.Then, when the function is called (createContact) and that function as an exceptions, PHP needs to catch it, other wise we will have this error...Sort of?
MEM
@MEM yes, that's it.
Artefacto
:D yupi!!! :D Thanks again for your patience. :D
MEM