- 论坛徽章:
- 0
|
这是怎么回事?
看来你误会了Class.forName(...)的作用了,Class.forName是给你用来确保一个class被load到当前的classloader中,并且完成初始化以被后续是用的.最常用的地方是load jdbc驱动程序。
见文档:
public static Class<?>; forName(String className)
throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.
For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread:
Class t = Class.forName("java.lang.Thread"
A call to forName("X" causes the class named X to be initialized.
从你的代码看
- public class test {
- public static void main(String[] args) {
- try {
- Class.forName("java.lang.String");
- System.out.println("hello");
- } catch (ClassNotFoundException e ){}
- }
- }
复制代码
你似乎没有了解try/catch在程序中的作用,如果你定义的函数有throw,那么你调用这个函数的命令则必须用try/catch包裹,但是并不意味着一定会throw的,如果不发生exception,就不会执行catch部分的代码,这是正常的,只有发生了exception才需要执行catch的代码,目的是为了使得程序运行的时候得到更多的控制。有时候exception发生并不意味着致命错误,你仍然可以用一些办法使其继续工作。
举个例子吧:
比如,你要从数据库读取一些数据,然后合并到一个xml文件中去,其中一些项目,如果xml文件中存在,就不要去修改,如果不存在,则写入数据库读出来的部分。这个逻辑写成程序的时候应该是这样:
- try{
- //1. load database driver
-
- try{
- //2. read xml file
- }catch(xmlexception){
- //如果xml不存在,可以在这里创建一个空的schema,但是操作要继续下去
- }catch(writefileexception e){
- //如果无法写入文件,就把exception往上级处理程序抛,从而强行中断后面的更新程序
- throw e;
- }
- //3. 数据同步/更新
- //这里的程序就不会再受到不能写入的困扰了。
- }catch(dbexception){
- //如果数据库无法访问,直接退出程序,不要继续了
- }catch(writefileexception e){
- //这里处理不能写入的问题
- }
复制代码
这样的结构就把错误处理层次化了。在反复嵌套的调用结构中,如果每一级都要处理上面遗留下来的可能的错误,会导致程序越来越不受控制。有个非常典型的例子:程序到数据库读取一个数据,读出来一个null,这里面有几种可能:
1.这个数据本身就是null
2.sql语句出错
3.数据库没服务器没有启动
4.从客户端到服务器网络不通
5.客户端可用的socket用光,无法创建socket连接对方。
。。。
如果没有层次化exception处理,判断这些可能就变得非常困难,但是有了层次化的exception处理,你会得到确切的错误信息,并且错误不会被继续下去而产生更多的没有意义的错误,比如,如果连不上数据库服务器,就不要去执行sql操作的部分,应该直接跳出。这里不能简单地exit退出,如果数据库可能是个集群系统,你可能想尝试别的服务器,然后返回最终的结果呢?Exception就是帮助你来区分发生问题的层次,进而更有效的控制程序用简单的方法,更少的代码来达到目的的工具。 |
|