什么时候应该try{}catch(){}
xgy_p 2004-03-31 02:33:35 一直想不明白的一个问题是,在java中到处什么时候应该要try{}catch{},什么时候不用。一般情况下认为,如果某个类的某个函数会throw Exception,则应该要用到try...catch块。例如在JDBC中的DriverManager.getConnection,Connection.createStatement,Statement.executeQuery等等,这些函数当然要try...catch啦。但是有的时候,明明会throw Exception,但不用try...catch也可以编译通过,例如最明显的是Double.parseDouble(String),Integer.parseInt等等,查manual可以看出这些函数也会throws NumberFormatException。但居然不用try...catch也照样可以。谁能准确地告诉我在哪些时候应该用try...catch块,哪些时候不必使用。