Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:33:34+00:00 2026-06-01T12:33:34+00:00

I have never used Try-catch in my code before, but now I need to

  • 0

I have never used Try-catch in my code before, but now I need to do it and it seems that I don’t quite understand the way it works. I have data in a string which I get using explode:

$groupNumbers = array();
$str = $dataGroups['groups'];
$groupNumbers = explode(",", $str);
$count = count($groupNumbers);

Then I want to check if every element is numeric, and if it is I proceed with a database query, otherwise I want to abort the action ant return some error.

Here is what I’m doing:

for ($i = 0; $i < $count; ++$i)
    {
        try
        {
            is_numeric($groupNumbers[$i]);

        }
        catch (Exception $ex)
        {
            process_exception_to_json($ex);
        }
    }

and if evrery element is numeric I construc an active record to do SQL:

$this->db->insert_batch(‘users_groups’, $datas);

Obviously written like that, even if an element is not numeric the action is not aborted and I the insert_batch is still executed with unvalid values which is waht I want to avoid. What is the exact way of doing this, so I can get an exception, and abort the action at the same time.
Thanks

Leron

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:33:35+00:00Added an answer on June 1, 2026 at 12:33 pm

    This is because is_numericDocs does not throws an exception but returns a value. It will never fail / throw an exception.

    You would need to do it this way (just an example, I don’t suggest it as it’s superfluous):

    try
    {
        if (!is_numeric($groupNumbers[$i])) {
            throw new RuntimeException('Not numeric.');
        }
    
    }
    catch (Exception $ex)
    {
        process_exception_to_json($ex);
    }
    

    Instead create your own group numbers type:

    try
    {
        $groupNumbers = GroupNumbers::createFromString($dataGroups['groups']);
    }
    catch (Exception $ex)
    {
        process_exception_to_json($ex);
    }
    

    With the following type:

    class GroupNumbers extends ArrayObject
    {
        public function construct(Array $numbers) {
            foreach ($numbers as $number)
            {
                if (!is_numeric($number))
                {
                    throw new InvalidArgumentException(sprintf('Not numeric "%s".', $number));
                }
            }
            parent::__construct($numbers);
        }
        public static function createFromString($string) {
            return new self(explode(",", $string));
        }
    }
    

    The string processing is now encapsulated, no more outer-loops and GroupNumbers does only instantiate if there are actual numbers in the string.

    If you’re not fluent with classes, you can have similar benefits as well with a procedural style. It’s probably easier to understand (but pretty much the same):

    try
    {
        $groupNumbers = GroupNumbers_createFromString($dataGroups['groups']);
    }
    catch (Exception $ex)
    {
        process_exception_to_json($ex);
    }
    
    function GroupNumbers_createFromString($string)
    {
        $numbers = explode(",", $string);
        foreach ($numbers as $number)
        {
            if (!is_numeric($number))
            {
                throw new InvalidArgumentException(sprintf('Not numeric "%s".', $number));
            }
        }
        return $numbers;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying to understand that - I have never used it before. How is
I have never used Windsor before but have used other DI frameworks, and I
I have never used octal numbers in my code nor come across any code
I have never used Perl, but I am really impressed by the ack ,
I have never used array_combine before and I am getting a Boolean instead of
I am new ASP.NET and I have never used a GridView or DataGrid, but
I have two distinct modules that can be used independently, but Module2 is dependent
I've used try and catch statements as an easy way to keep my code
So, I have never used devise before and am trying to implement it in
I have never used any ORM, but the Zend_Db_Table (if you can call it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.