SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MVC exception handling

    hi I am wondering the proper way to implement exceptions in a MVC environment. Mainly the best way to use try and catch in the controller.

    for instance would it be correct to do something like when setting up elements for the view:

    PHP Code:
    try {
      
    $news = new News($catagory_id);
      
    $news->get_latests();

      
    $members = new Members();
      
    $members->get_new_members();

      
    $blah = new Blah();
      
    $blah->whatever_else();
    }
    catch (
    Exception $e) {
      
    // whatever here
    }
    catch (
    specialException $se) {
      
    // special whatever

    and then just throw the exceptions in the objects? some times when setting up a page I will have tons of objects being used. Shoudl they be handled like this or on a per object bases? Thoughts.

    Thanks.

  2. #2
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have some code like that, but in the front controller that actually instantiates the object and calls the function to execute the requested action, I have a try/catch block over that whole thing that will display a generic error page if it catches a previously uncaught exception. That way, I don't always have to do a try/catch block in every controller - just the times I need special handling.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea some exceptions i let bubble up to the main controller where appropriate error page can be shown

  4. #4
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, so no one has any objections to this? Thanks

  5. #5
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well at least your handling exceptions and your mind is thinking of exceptions/issues that arise

    more than i can say for most php developers out there

  6. #6
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    692
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Just be aware that your catch SpecialExceptions needs to come before the catch Exceptions statement.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •