views:

98

answers:

3

I am getting unexpected T_TRY, expecting T_FUNCTION error message and am not sure as too why am getting that, can't we use try and catch block inside class like this:

class Processor
{

    protected $dao;

    protected $fin;

    try
    {
        public function __construct($file)
        {
            //Open File for parsing.
            $this->fin = fopen($file,'w+') or die('Cannot open file');
            // Initialize the Repository DAO.
            $this->dao = new Dao('some name');
        }

        public function initiateInserts()
        {
            while (($data=fgetcsv($this->fin,5000,";"))!==FALSE)
            {
                $initiate_inserts = $this->dao->initiateInserts($data);
            }
        }
        public function initiateCUpdates()
        {
            while (($data=fgetcsv($this->fin,5000,";"))!==FALSE)
            {
                $initiate_updates = $this->dao->initiateCUpdates($data);
            }
        }
        public function initiateExecuteIUpdates()
        {
            while (($data=fgetcsv($this->fin,5000,";"))!==FALSE)
            {
                $initiate_updates = $this->dao->initiateIUpdates($data);
            }
        }
    }
    catch (Exception $e)
    {
    }
}
+2  A: 

You can't have any "do this stuff"-code in a class outside of a method. There is nothing to "try to do" inside those curly brackets, because the stuff inside is just method definitions.

Coronatus
Than how can I implement try and catch block which would cover all the methods because potentially all methods can throw exception and so I was capturing all methods inside try block and trying to do something in catch block.
Rachel
@Rachel: Not even one of the method throws any exceptions. You don't have anything to catch.
Coronatus
they can possible because they are interacting with db, please don't mind my mere knowledge but as am new to programming my fundas are not that clear.
Rachel
You could make all the functions private and then use the magic functions __call to run all calls through a try/catch block. But this should really be another question.
Kendall Hopkins
@Kendall: That seems to be a quite complex approach for a simple problem.
Rachel
+3  A: 

You can't put all your method definitions into one try-catch block.

Instead of

class Foo {
  try {
    public function func1() { }
    public function func2() { }
  }
  catch(Exception $e) {
  }
}

you have to use

class Foo {
  public function func1() {
    try {
      ...
    }
    catch(Exception $e) {
      ...
    }
  }

  public function func2() {
    try {
      ...
    }
    catch(Exception $e) {
      ...
    }
  }

}
VolkerK
Can I have different try blocks for each method but one catch block to catch all the exception that are being thrown potentially by all methods ?
Rachel
in that case, will e->getMessage() be combined of all exception traces or will it be on individual basis, actually i want to logs all exception trace.
Rachel
Not sure exactly what you want to achieve. But if you don't catch an exception it "bubbles up" the call stack. Anywhere within that stack you can catch the exception, even do stuff and then re-throw it (let it continue bubbling up). So, depending on your actual goal you _might_ be interested in http://docs.php.net/set_exception%20handler
VolkerK
Actually I am doing same, but the thing is that I need to capture exception trace information and collect it along with the current database state and so I cannot do both in same file and so am looking for other options available.
Rachel
"Can I have different try blocks for each method but one catch block" - You can call the same method in each catch block. If you pass the exception object as a parameter you can also re-throw it from that method.
VolkerK
+2  A: 

Don't try-catch inside of each method, you could simply try-catch when you use your class:

try {
    $p = new Processor($file);
    $p->initiateInserts();
    $p->initiateCUpdates();
    // and so on...
} catch (Exception $e) {
    // handle the error...
}

This way your class will be much cleaner and you can decide what to do with errors. Especially if you use your class in multiple places - you can have customized error handling for each case.

Laimoncijus
Actually I am doing same, but the thing is that I need to capture exception trace information and collect it along with the current database state and so I cannot do both in same file and so am looking for other options available.
Rachel