n**********2 发帖数: 214 | 1 What is stale object in Java? How will you handle it? For example: You have
a Class A as shown below
public class A{
A(){
// code for database connection
}
// code for other method
}
Now,you are trying to create a object A by its constructor. To initialize
constructor it throws some exception to create database connection.
A obj = new A().
obj.amethod() ;
If obj.amethod() ; will be executed successfully or not ?
How will you stop your code not to use obj? |
d****i 发帖数: 4809 | 2 just use try-catch pair:
try {
A obj = new A();
obj.aMethod();
} catch (Exception ex) {
//handle exception here
}
have
【在 n**********2 的大作中提到】 : What is stale object in Java? How will you handle it? For example: You have : a Class A as shown below : public class A{ : A(){ : // code for database connection : } : // code for other method : } : Now,you are trying to create a object A by its constructor. To initialize : constructor it throws some exception to create database connection.
|
n**********2 发帖数: 214 | 3 大牛能稍微解释下嘛?原理是什么
【在 d****i 的大作中提到】 : just use try-catch pair: : try { : A obj = new A(); : obj.aMethod(); : } catch (Exception ex) { : //handle exception here : } : : have
|
d****i 发帖数: 4809 | 4 就是异常处理,如果try block的代码抛出异常,那么catch block就抓到后处理异常。
详情参见文档:
http://docs.oracle.com/javase/tutorial/essential/exceptions/
【在 n**********2 的大作中提到】 : 大牛能稍微解释下嘛?原理是什么
|