c#,catch(Exception ex)的ex是什么意思

catch(Exception ex)
{
MessageBox.Show(ex.Message);
ex就表示捕获的异常类对象。
展开阅读全文

catch (Throwable ex)的问题

07-27

在看关于hibernate的书的时候有上面这段代码,然后用sonarqube分析过后对于静态代码块部分(大约15-20行的位置)报了 Catch Exception instead of Throwable.rn上网查了一下ExceptionInInitializerError是一个error,按照道理出现这个问题jvm应该就是死了,那么还有必要catch(Throwable)以及throw new ExceptionInInitializerError(ex);吗?rn[code=java]package util;rnrnimport org.hibernate.HibernateException;rnimport org.hibernate.Session;rnimport org.hibernate.SessionFactory;rnimport org.hibernate.cfg.Configuration;rnrnpublic class HibernateUtil rnrn private static SessionFactory sessionFactory;rn private static Configuration configuration = new Configuration();rn // 创建线程局部变量threadLocal,用来保存Hibernate的Sessionrn private static final ThreadLocal threadLocal = new ThreadLocal();rn // 使用静态代码块初始化Hibernatern static rn try rn Configuration cfg = new Configuration().configure(); // 读取配置文件hibernate.cfg.xmlrn sessionFactory = cfg.buildSessionFactory(); // 创建SessionFactoryrn catch (Throwable ex) rn throw new ExceptionInInitializerError(ex);rn rn rnrn // 获得SessionFactory实例rn public static SessionFactory getSessionFactory() rn return sessionFactory;rn rnrn // 获得ThreadLocal 对象管理的Session实例.rn public static Session getSession() throws HibernateException rn Session session = (Session) threadLocal.get();rn if (session == null || !session.isOpen()) rn if (sessionFactory == null) rnrn rebuildSessionFactory();rn rn // 通过SessionFactory对象创建Session对象rn session = (sessionFactory != null) ? sessionFactory.openSession() : null;rn // 将新打开的Session实例保存到线程局部变量threadLocal中rn threadLocal.set(session);rn rn return session;rn rnrn // 关闭Session实例rn public static void closeSession() throws HibernateException rn // 从线程局部变量threadLocal中获取之前存入的Session实例rn Session session = (Session) threadLocal.get();rn threadLocal.set(null);rn if (session != null) rn session.close();rn rn rnrn // 重建SessionFactoryrn public static void rebuildSessionFactory() rn try rn configuration.configure("/hibernate.cfg.xml"); // 读取配置文件hibernate.cfg.xmlrn sessionFactory = configuration.buildSessionFactory(); // 创建SessionFactoryrn catch (Exception e) rn System.err.println("Error Creating SessionFactory ");rn e.printStackTrace();rn rn rnrn // 关闭缓存和连接池rn public static void shutdown() rn getSessionFactory().close();rn rnrn[/code] 论坛

没有更多推荐了,返回首页