Welcome Guest, Not a member yet? Register   Sign In
Error Handling
#1

[eluser]Valdemar[/eluser]
Hi,

I'm quite new to CI framework and am still exploring its options. One thing that bothers me is that it doesn't seems to have any type of error handling.

As a Java user, I'm pretty used to try->catch mechanism. Am I overlooking it or is it not implemented in the framework? If not, is there any similar alternative implemented in CI?

Thank you for your time.
#2

[eluser]coolfactor[/eluser]
You are comparing CI (a framework written in PHP) to Java (a programming language)? You might want to compare apples to apples instead. In this case, PHP is where the try->catch mechanism needs to exist, and it does in PHP 5.
#3

[eluser]coolfactor[/eluser]
And as for error handling, CI has an Exceptions class that you can extend for your own needs.
#4

[eluser]Valdemar[/eluser]
[quote author="coolfactor" date="1186061950"]You are comparing CI (a framework written in PHP) to Java (a programming language)? You might want to compare apples to apples instead. In this case, PHP is where the try->catch mechanism needs to exist, and it does in PHP 5.[/quote]

Not really, I'm comparing the functionalities of the error handling built into Java with that provided in CodeIgniter. Different languages cannot be compared whereas similar functionalities can.

Further more try->catch is a common way of handling errors and is included in many languages, including php.
#5

[eluser]coolfactor[/eluser]
Yes, but try->catch not a function of CodeIgniter, but rather the underlying programming language, and it's already there, at least under php5.
#6

[eluser]mrahman[/eluser]
Hi. I was searching the forum for 'exception handling' before asking this question...
Let me explain two scenarions:
1)
The first application i'm building with CI so far, is built with multilanguage support in the first hand. That is, i had to separate the fiew files each under a directory named as the language (english, arabic, russian, etc...). all resources like menus and images, all placed under its language. with one cool exception, the default language in $config['language'] is used whenever the requested url, in the selected language, was 404. Then the alternate default language file will be instead.

So, i had to check if the file_exists first. so i don't get the show_404, and load the default one instead.
And this file_exists() funciton, is what the framework does before loading the final viewfile, (i checked before, i'm sure it exists). now the application as a unit, checked for a single file, twice!

this is one of the benifits of errors. i imagined i could write:
Code:
try
{
    $this->load->view($requestedfile);
}
catch (FileNotFoundException $e)
{
    try
    {
       $this->load->view($englishfile);
    }
    catch (FileNotFoundException $e404)
    {
        show_404();
    }
}

Just imagined, because the loader class just show_404() and exit.

2)
A worse scenario could be when you are inserting a record in a table, with a primary key (say the ID field, and our new row has ID=100), you would check to see, for a duplicated pk value( a record with id=5), running a select count where id=5... The good or bad news is mysql will surely check for that pk violation anyway. even mysql checking procedure is definitely optimized than mine.
You could catch the constraint exception, a single check would be enough.
But now, we and our friend mysql made checking for id=5, twice!

That's exceptions throwing... And then my opinion is making the framework graceful is not less important than being programmer friendly. In the two previous scenarios, that double checking decreases performance, hunting this sweet speed optimized framework.
Do you agree on this? is it what you need exception handling for?
Exception handling is new to PHP5.
Many greetings for CodeIgniter.
oh, so long message. sorry. sorry. twice!
#7

[eluser]Jose Dueñas[/eluser]
I would like to know opinions about your error handling approaches.

Thanks,
Jose
#8

[eluser]mrahman[/eluser]
Within classes, I supress errors and check for functions' return values and accordingly throw custom exceptions, for example
Code:
function loadData($filename)
{
$data=@file_get_contents($filename);
if(empty($data))
     throw new Exception('file is empty or not found');
return $data;
}

and use try/catch block in consumer
#9

[eluser]jdfwarrior[/eluser]
Agreeing with coolfactor. Your not comparing the same thing if your comparing java to codeigniter, as codeigniter is only a framework built with php and java is an actual language. Try-catch exists in CodeIgniters underlying language (PHP).
#10

[eluser]Jose Dueñas[/eluser]
[quote author="mrahman" date="1235432375"]Within classes, I supress errors and check for functions' return values and accordingly throw custom exceptions, for example
Code:
function loadData($filename)
{
$data=@file_get_contents($filename);
if(empty($data))
     throw new Exception('file is empty or not found');
return $data;
}

and use try/catch block in consumer[/quote]

I use this in my controller:

Code:
function printArticle($id=0)
    {
        
        if($id==0)
        {    
            show_error('Article not found!');
        }else{
            $this->load->view('articles_print');            
        }
    }

Maybe is better to use the CI error handling class?

Anyway I like your approach.

Regards,
Jose




Theme © iAndrew 2016 - Forum software by © MyBB