NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Find uncaught Exeptions


Joined: 09 Feb 2009
Posts: 5
Reply with quote
Hi,

is there a way to find uncaught exceptions globally/per method?

Thanks,
Ingmar
View user's profileFind all posts by ingmar.heinrichSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
Exceptions are raised in Run-Time (e.g. while your scripts are running) and you can test your code in order to see if they are properly handled.

Certainly you can statically (without any execution) analyze your scripts in order to find out where the exceptions are generated and where they are caught, but in-general it's very hard to say if all possible combinations are properly handled and no one exception could be left uncaught.

I think it's easier to generate test-cases with adequate code coverage, run them and analyze the results.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 09 Feb 2009
Posts: 5
Reply with quote
It's obvious that use cases for automated testing should be applied in order to find all exceptions, but proper IDEs do this on a code analysis level too. I assume PHPED can't do this? If so, is it planned as a feature?

Thanks,
Ingmar
View user's profileFind all posts by ingmar.heinrichSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
no, with dynamic languages like php it's hardly possible to analyze code branches and find the exceptions that aren't properly cautch in particular. Because of php's type-less nature, it's almost always unknown what class instances are stored in the variables. Sometimes it's not even known what methods are called. Just for example:

Code:
function A($cls, $mth, $arg) {
  $a = new $cls;
  $a->$mth($arg);
}

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 09 Feb 2009
Posts: 5
Reply with quote
Code:
function A($cls, $mth, $arg) {
  $a = new $cls;
  $a->$mth($arg);
}


Well, but if it looked like this:

Code:
function A($cls, $mth, $arg) {
  if($cls === NULL) { throw new SpecialException('whoopie'); }
  $a = new $cls;
  $a->$mth($arg);
}


, I should be able to scan my project folder for calls to A() that don't catch SpecialException, shouldn't I? Of course I'm screwed when it comes to things like dynamic method naming, but the exception converage of static code would be of great help. Especially if you could also ask for cases of too general exception handling like

Code:
try {
  A();
} catch (Exception $e) {
    echo 'Caught Exception: ',  $e->getMessage(), "\n";
}
View user's profileFind all posts by ingmar.heinrichSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
Quote:
I should be able to scan my project folder for calls to A() that don't catch SpecialException, shouldn't I?


Well, there are two distinct perspectives, the caller's and the callee's. When you see a particular method, you always know whether it can generate an exception and it's not cautch locally. So you just need to see where this method is called from. It's where you get all the problems with unknown methods/unknown classes. With my example, you can not be sure that method called in the function A is not the method that generates an exception.
Well, even if you know method name, it's not helpful because you need classname too and it's problematic with many PHP code available from many php libraries/frameworks/cms.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 09 Feb 2009
Posts: 5
Reply with quote
You are still true about 'your case' that implies dynamic object generation, but if 'my case' would be covered, I'd already be pretty happy. Meaning, that all instances of code that's explicitly calling A(), which do not handle SpecialException, get reported to me.
View user's profileFind all posts by ingmar.heinrichSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
Quote:
Meaning, that all instances of code that's explicitly calling A(), which do not handle SpecialException


ok. What's about all instances of code that call these instances of code calling A()? For example:

Code:
function B() { // does not handle "SpecialException" generated by A
  A("foo", "foo", "foo");
}

function C() {
  try {
     B();
  }
  catch (SpecialException $e) {
    ...
  }
}


Should B() be found as non-handling SpecialException generated by A?

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 09 Feb 2009
Posts: 5
Reply with quote
Code:
function B() { // does not handle "SpecialException" generated by A
  A("foo", "foo", "foo");
}

function C() {
  try {
     B();
  }
  catch (SpecialException $e) {
    ...
  }
}


In this case, no. But in this case, D would be spotted:

Code:
function B() { // does not handle "SpecialException" generated by A
  A("foo", "foo", "foo");
}

function C() {
  try {
     B();
  }
  catch (SpecialException $e) {
    ...
  }


function D() {
  B();
}


The system would have to find any throwing of an Exception and traverse all the way up.
View user's profileFind all posts by ingmar.heinrichSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
thanks, It's clearer now.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website
Find uncaught Exeptions
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic