Discussion in 'Website Development & Design' started by beingchinmay, Dec 29, 2015.

  1. beingchinmay

    uix_expand uix_collapse
    New Member

    Aug 4, 2015
    Likes Received:
    Exception handling tends to be one of the more problematic aspects in software development. Not only is it hard for the developer to decide what to do when an error occurs (such as database failure, network failure, or a software bug), but it is hard to spot all the places in the code to insert checks for failure and to call the correct function to handle it. An even more complicated task is that after you handle the failure, how do you fix your program’s flow to continue at a certain point in your program?

    Today, most modern languages support some variant of the popular try/ catch/throw exception-handling paradigm. try/catch is an enclosing language construct that protects its enclosing source codeand basically tells the language, “I’m handling exceptions that occur in this code.” Exceptions or errors are “thrown” when they are detected and the language run time searches its call stack to see if there is a relevant try/catch construct that is willing to handle the exception.

    There are many advantages to this method. To begin with, you don’t have to place if() statements in every place where an exception might occur; thus, you end up writing a lot less code. Instead, you can enclose the entire section of code with a try/catch construct and handle an error if one occurs. Also, after you detecte an error using the throw statement, you can easily return to a point in the code that is responsible for handling and continuing execution of the program, because throw unwinds the function call-stack until it detects an
    appropriate try/catch block.

    The syntax of try/catch is as follows:

    The try {} construct encloses the code that can throw an exception,which is followed by a series of catch statements, each declaring what exception class it handles and under what variable name the exception should be accessible inside the catch block .

    When an exception is thrown, the first catch() is reached and an instance of comparison with the declared class is performed. If the result is true, the catch block is entered and the exception is made available under the declared variable name. If the result is false, the next catch statement is checked. Once a catch statement is entered, the following catch statements will not be entered, even if the instanceof check would result in true. If no catch statements are relevant, the language engine checks for additional enclosing try/
    catch statements in the same function. When none exist, it continues searching by unwinding the call stack to the calling functions.

    The throw statement

    can only throw an object. You can’t throw any basic types such as strings or integers. A pre-defined exception class exists called Exception, from which all your exception classes must inherit. Trying to throw an object which does not inherit from class Exception will result in a final runtime error.

    The following code snippet shows the interface of this built-in exception class (the square brackets in the constructor declaration are used to represent optional parameters, which are not valid PHP syntax):

    The following is a full-blown example of exception handling:

    • Like Like x 1
  2. alexdisuza

    uix_expand uix_collapse

    Jun 19, 2013
    Likes Received:
    Simply, Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs.

Share This Page