0

I am Studying about Exceptions At W3schools At this Link, inside the link just before the heading:

"Re-throwing Exceptions"

there is a sentence that say:

"If the exception thrown were of the class customException and there were no customException catch, only the base exception catch, the exception would be handled there."

If can someone please give me an example for this sentence i will be very thankful.

2
  • 9
    w3fools.com Commented Jun 1, 2012 at 16:52
  • Know this website, Everyone thank you i will read your answers thanks. Commented Jun 1, 2012 at 17:02

4 Answers 4

7

Basically they're saying if you don't have a catch statement set up to catch customException it will fall through to the general Exception catch statement.

In this example, the first catch statement will catch customException because it explicitly is designed to do so.

try {
    // fail
    throw new customException();
}
catch (customException $e) {
    // catch custom exception
}
catch (Exception $e) {
    // catch any uncaught exceptions
}

In the next example, because that clause it missing the generic Exception catch block will catch it instead:

try {
    // fail
    throw new customException();
}
catch (Exception $e) {
    // catch any uncaught exceptions
}
Sign up to request clarification or add additional context in comments.

Comments

3

Take a look at Example #2 on this page of the PHP manual

And w3schools isn't the best place to learn PHP, it's (in)famous for its errors

Comments

2

Say you have a custom exception defined as

class CustomException extends Exception.

Now somewhere in your code:

try
  {
  // do something that throws a CustomException
  }

catch(Exception $e) // you are only catching Exception not CustomException,
// because there isn't a specific catch block for CustomException, and
// because Exception is a supertype of CustomException, the exception will 
// still be caught here.
  {
  echo 'Message: ' .$e->getMessage();
  }

So what it means is that because CustomException is a subclass of Exception, as long as you catch the Exception type of the superclass without a catch block for the more specific sub class type, it will still be caught

5 Comments

It makes no sense, is it special for Exceptions?
Not really, what you are seeing here is just a form of Polymorphism.
But when you extends for example: class a extends class b, if i create object from class a i cant access class b prop/methods. I learned about Polymorphism and its looking the same but dosnt make any sense at all can you expand please...
If class a extends b, and if you create an instance of class a, then you should be able to access class b methods. It is the other way around that won't work, meaning from an instance of class b, you cannot call methods of the subclass. The reason is simple. The child class (class a) is aware of its parent's methods. But the class b has no idea class a even exists.
So it does make sense now when you saying Polymorphism, Thank you and Have a Nice Day.
2

There are two catch blocks in the example. The first catches only customException; the next one catches any Exception. If the first block catches anything, you never get to the second block. But since a customException is also an example of an Exception, it would have gotten caught there if we didn't have the first catch block. This way, the first catch catches it, and the execution never reaches the second.

In the meantime, read the link I posted above, and why w3schools is a bad idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.