LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2010, 09:07 AM   #1
metalenkist
Member
 
Registered: Aug 2005
Distribution: OpenSuse 11.1
Posts: 36

Rep: Reputation: 15
ZCE Exceptions


Hey all, at the moment I'm studying for my ZCE (Zend Certified Engineer) exam and there and there are a few questions that are real headbreakers for me. One is about exceptions and hopefully can one of you explain why the output of the following code is "Didn't Caught Exception".

Already thanks a lot.

Code:
<?php
class MyException extends Exception {}
class AnotherException extends MyException {}

class Foo {
  public function something() {
    throw new AnotherException();
  }
  public function somethingElse() {
    throw new MyException();
  }
}

$a = new Foo();

try {
  try { $a->something();}
    catch(AnotherException $e) { $a->somethingElse();}
    catch(MyException $e) { print "Caught Exception";}
} catch(Exception $e) {
  print "Didn't catch the Exception!";
}

?>
 
Old 04-03-2010, 09:26 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by metalenkist View Post
Hey all, at the moment I'm studying for my ZCE (Zend Certified Engineer) exam and there and there are a few questions that are real headbreakers for me. One is about exceptions and hopefully can one of you explain why the output of the following code is "Didn't Caught Exception".

Already thanks a lot.

Code:
<?php
class MyException extends Exception {}
class AnotherException extends MyException {}

class Foo {
  public function something() {
    throw new AnotherException();
  }
  public function somethingElse() {
    throw new MyException();
  }
}

$a = new Foo();

try {
  try { $a->something();}
    catch(AnotherException $e) { $a->somethingElse();}
    catch(MyException $e) { print "Caught Exception";}
} catch(Exception $e) {
  print "Didn't catch the Exception!";
}

?>
For starters, reformat the code:

Code:
try
  {
  try
    {
    $a->something();
    }
  catch(AnotherException $e)
    {
    $a->somethingElse();
    }

  catch(MyException $e)
    {
    print "Caught Exception";
    }
  }
catch(Exception $e)
  {
  print "Didn't catch the Exception!";
  }
 
Old 04-03-2010, 10:11 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I think that you need to put another try block around the call to $a->somethingElse();
 
Old 04-03-2010, 10:32 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by graemef View Post
I think that you need to put another try block around the call to $a->somethingElse();
Yes, because it causes yet another exception.
 
Old 04-03-2010, 01:09 PM   #5
metalenkist
Member
 
Registered: Aug 2005
Distribution: OpenSuse 11.1
Posts: 36

Original Poster
Rep: Reputation: 15
At the mockup exam you get 4 options and you have to pick one. I taught myself the rule:

if a catch block throws an exception then ignore that catch block & and look to the upper catch.

Code:
catch(AnotherException $e)
    {
    $a->somethingElse();
    }
will throw error go to the next catch block

Code:
catch(MyException $e)
    {
    print "Caught Exception";
    }
This will never be caught cause it will catch exception from the MyException class.

Code:
catch(Exception $e)
  {
  print "Didn't catch the Exception!";
  }
This last catch, catches every error.

That's why I think the result is "Didn't catch the Exception!".
 
Old 04-03-2010, 09:42 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Okay I think that I misunderstood your original question.

As I understand it your thinking is correct.

When an exception is thrown within a try block the catch clauses will be tested in turn for a match (this works in the same way that a case clause within a switch statement works).

In the code that you have the catch clause itself throws an exception. So that throw looks for a try block, which means it needs to step out of its own try block into the containing try block and then look through the catch clauses for a match.

Earlier I compared the catch and cases clauses, the difference between them is when a match is found with the case clause it will still continue (unless you insert a break or return statement) whilst with the catch once a match is found it will stop. That is why the order of the catch clause is important. You need to place them in order of most specific through to most general.

Hope that helps some.
 
Old 04-07-2010, 04:33 PM   #7
metalenkist
Member
 
Registered: Aug 2005
Distribution: OpenSuse 11.1
Posts: 36

Original Poster
Rep: Reputation: 15
w00t, Yesterday I had the exam and I passed *woohooo*

Thank you very very much with helpen me out with this question.

There was a similar question at the exam.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions Mailserver maceee Linux - Server 7 02-02-2009 02:51 AM
Java Exceptions manolakis Programming 4 12-09-2007 06:03 AM
Exceptions bianchi Programming 1 11-17-2005 08:36 PM
Regarding Exceptions eshwar_ind Programming 1 05-03-2005 12:23 PM
exceptions of Python~ black Programming 2 09-09-2002 05:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration