Exception handling is a de facto language feature now, but different language has different view of it. Personally, I prefer handling exception with language feature other than logical code. It makes the code much easier to read.

This is a simple example in Python show a message when trying to open a file that not exist.

 
try:
    f = open("notexistfile")
 
except IOError as e:
    print "open file exception"
    print e
 

The open function will throw an exception if the file not exist. This is exactly behavior we want.

I want to see how PHP will do this.

 
<?php
 
try {
    $f = fopen("notexistfile" , "r");
} catch (Exception $e) {
    echo "open file exception";
    print_r($e);
}
?>
 

No exception caught. PHP just show a warning message. Looks like PHP didn't take exception very seriously. The built in function just ignore the exception handling mechanism of the language.

Actually, most PHP code I saw never use exception handling. Maybe handling exception is just not the PHP style.

In PHP's tradition, exceptions always represented by warning and error. But you can not catch it, and when it happening, you can do nothing about it.

Python do this much better than PHP, the exception handling mechanism is used everywhere, from interpreter error like syntax error to runtime error like invalid list index, all these are represented with exception.

This may add some burden for learning Python, but you can write more readable and maintainable code. Any serious language should provide a nice exception handle facilities.

Finally, I want to illustrate how Java will do this.

 
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
 
 
 
public class FileNotFoundExceptionTest {
    public static void main ( String [] args ) {
        File f = new File("notexistfile");
 
        try {
            FileReader r = new FileReader(f);
        } catch (FileNotFoundException e) {
            System.out.println("File open exception");
        }
    }
}
 
 

Without the try catch block, the code can not even pass the compiler check. This is called checked exception. The exception declared in method signature must be handled .

The checked exception is still controversial, Python didn't enforce it, if an exception is not caught by the program, the interpreter always handle it.

The order of strictness of exception handling: PHP < Python < Java .