Jump to content

How to display multiple exceptions at the same time?


Koobi

Recommended Posts

I've just recently started really exploring the new features of PHP 5.

 

I'm writing a validation exception class that extends PHP's built-in base exception class.

 

 

Here's a simple example:

class Validation
{
    function __construct() {
    }

    public validate_int ($value) {
        if (FALSE === ctype_digit($value)) {

            //assume that Validation_Exception is a class that exists that handles exceptions and is very similar
            //to the standard exception class.
            throw new Validation_Exception('Invalid integer input', SOME_CONSTANT_VALUE);
        } else {
            return TRUE;
        }
    }

    public validate_alnum ($value) {
        if (FALSE === ctype_alnum($value)) {
            throw new Validation_Exception('Invalid alphanumeric input', SOME_OTHER_CONSTANT_VALUE);
        } else {
            return TRUE;
        }
}

try {
    $val = new Validation();
    $val->validate_int('5d');
    $val->validate_alnum('5.2');
} catch (Exception_Validation $e) {
    echo $e->__toString();
}

 

 

Since both inputs are not valid, the validation should fail.

The way it's currently structured, I only recieve one error message at a time.

 

What is the most effective/flexible way to display all exceptions at the same time?

 

 

Thanks for reading.

try {} blocks will go straight to the catch block the first time an error is encountered.  The best way to go about what you are doing is to have the function return a value instead of just using a try{} block.  Like this:

 

if (!someFunction($input)) { print "Something didn't process corretly"; }

And continue using if statements.  Just have your function return true or false based on the input value.  Or, you can have it return true or return an error message if something failed.

wow so sorry, i forgot about this thread.

 

thanks for the replies :)

 

 

 

try {} blocks will go straight to the catch block the first time an error is encountered.  The best way to go about what you are doing is to have the function return a value instead of just using a try{} block.  Like this:

 

if (!someFunction($input)) { print "Something didn't process corretly"; }

And continue using if statements.  Just have your function return true or false based on the input value.  Or, you can have it return true or return an error message if something failed.

 

that would mean not using try/catch at all. so then PHP5 can't trap multiple exceptions?

 

 

 

How I like to do error handling is build an array.  Everytime an error comes up, throw it into the array as a new entry and at the end of all the validation, see if anything is in that array, if so, kill the script and print the array.

yeah, i implemented that in PHP4 regularly using set_error_handler() and trigger_error() but PHP5 has exceptions so i want to use that.

 

 

 

how would you go about trapping multiple exceptions in Java?

here's an example:

 

<?php


class My_Validation
{
    function __construct () {
    }
    
    public function valid_numeric ($val) {
        if (ctype_digit($val)) {
            return TRUE;
        } else {
            throw new Exception('Not a valid digit', SOME_ERROR_CODE_CONSTANT);
            return FALSE;
        }
    }


    public function valid_alphanumeric ($val) {
        if (ctype_alnum($val)) {
            return TRUE;
        } else {
            throw new Exception('Not a valid alphanumeric', SOME_OTHER_ERROR_CODE_CONSTANT);
            return FALSE;
        }
    }

    function __destruct () {
    }

}


$validation = new My_Validation;

try {

    $validation->valid_numeric('4f');
    $validation->valid_alphanumeric('5.r');
    
} catch (Exception $error) {
    echo '<pre>' . print_r($error, true) . '</pre>';
}


?>

 

 

The problem is, $error only contains the first exception thrown by My_Validation::valid_numeric.

I want both throws to be caught there so that I can display them.

You don't catch multiple errors.  The purpose of try/catch is to stop the try statement as soon as the first exception is encountered, and then run the catch block of code.  It is intended to work the way it works, and I do not believe it has any method of capturing multiple exceptions.

perhaps

 

<?php 
class My_Validation
{
    public $excepts;
    
    function __construct () {
        $this->excepts = array();
    }
    
    public function valid_numeric ($val) {
        if (ctype_digit($val)) {
            return TRUE;
        } else {
            $this->excepts[] =  new Exception('Not a valid digit', 0);
            return FALSE;
        }
    }


    public function valid_alphanumeric ($val) {
        if (ctype_alnum($val)) {
            return TRUE;
        } else {
            $this->excepts[] = new Exception('Not a valid alphanumeric', 0);
            return FALSE;
        }
    }

    function __destruct () {
    }

}


$validation = new My_Validation;

try {

    $validation->valid_numeric('4f');
    $validation->valid_alphanumeric('5.r');
    
    if ($validation->excepts) {
        foreach ($validation->excepts as $e)
            echo  $e->getmessage().'<br>';
    }
    
} catch (Exception $error) {
    echo '<pre>' . print_r($error, true) . '</pre>';
}


?>

You don't catch multiple errors.  The purpose of try/catch is to stop the try statement as soon as the first exception is encountered, and then run the catch block of code.  It is intended to work the way it works, and I do not believe it has any method of capturing multiple exceptions.

 

thanks Glyde. i thought that exceptions was meant for this sort of thing. what is it really meant for anyway?

 

 

 

perhaps

 

<?php 
class My_Validation
{
    public $excepts;
    
    function __construct () {
        $this->excepts = array();
    }
    
    public function valid_numeric ($val) {
        if (ctype_digit($val)) {
            return TRUE;
        } else {
            $this->excepts[] =  new Exception('Not a valid digit', 0);
            return FALSE;
        }
    }


    public function valid_alphanumeric ($val) {
        if (ctype_alnum($val)) {
            return TRUE;
        } else {
            $this->excepts[] = new Exception('Not a valid alphanumeric', 0);
            return FALSE;
        }
    }

    function __destruct () {
    }

}


$validation = new My_Validation;

try {

    $validation->valid_numeric('4f');
    $validation->valid_alphanumeric('5.r');
    
    if ($validation->excepts) {
        foreach ($validation->excepts as $e)
            echo  $e->getmessage().'<br>';
    }
    
} catch (Exception $error) {
    echo '<pre>' . print_r($error, true) . '</pre>';
}


?>

 

that would work fine, thanks.

 

 

 

 

but do you think it's best practice to throw the exception inside my validation class or within the procedural code, like this:



class My_Validation
{
    function __construct () {
    }
    
    public function valid_numeric ($val) {
        if (ctype_digit($val)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }


    public function valid_alphanumeric ($val) {
        if (ctype_alnum($val)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function __destruct () {
    }

}


$validation = new My_Validation;

try {

    if ($validation->valid_numeric('4f') === FALSE) {
        throw new Exception('Not a valid digit', SOME_ERROR_CODE_CONSTANT);
    }

    if ($validation->valid_alphanumeric('5.r') === FALSE) {
        throw new Exception('Not a valid alphanumeric', SOME_OTHER_ERROR_CODE_CONSTANT);
    }
    
} catch (Exception $error) {
    echo '<pre>' . print_r($error, true) . '</pre>';
}


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.