{"record":{"id":"4fa302f0936cad6f","repo":"alibaba/druid","slug":"create-exceptionsorter-error","errorCode":null,"errorMessage":"create exceptionSorter error","messagePattern":"create exceptionSorter error","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java","lineNumber":1380,"sourceCode":"        if (exceptionSorter == null) {\n            this.exceptionSorter = NullExceptionSorter.getInstance();\n            return;\n        }\n\n        exceptionSorter = exceptionSorter.trim();\n        if (exceptionSorter.length() == 0) {\n            this.exceptionSorter = NullExceptionSorter.getInstance();\n            return;\n        }\n\n        Class<?> clazz = Utils.loadClass(exceptionSorter);\n        if (clazz == null) {\n            LOG.error(\"load exceptionSorter error : \" + exceptionSorter);\n        } else {\n            try {\n                this.exceptionSorter = (ExceptionSorter) clazz.newInstance();\n            } catch (Exception ex) {\n                throw new SQLException(\"create exceptionSorter error\", ex);\n            }\n        }\n    }\n\n    @Override\n    public List<Filter> getProxyFilters() {\n        return filters;\n    }\n\n    public void setProxyFilters(List<Filter> filters) {\n        if (filters != null) {\n            this.filters.addAll(filters);\n        }\n    }\n\n    public String[] getFilterClasses() {\n        List<Filter> filterConfigList = getProxyFilters();\n","sourceCodeStart":1362,"sourceCodeEnd":1398,"githubUrl":"https://github.com/alibaba/druid/blob/fa8dc9912637a2f729eef9f55356621fec18d40e/core/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java#L1362-L1398","documentation":"setExceptionSorter loads a class via Utils.loadClass and instantiates it via clazz.newInstance(), casting to ExceptionSorter. If the class loads but instantiation fails (abstract, inaccessible, constructor throws) or the cast fails, the cause is wrapped as SQLException(\"create exceptionSorter\", ex). A null loadClass result is a separate LOG.error path that does NOT throw.","triggerScenarios":"setExceptionSorter(\"com.example.MySorter\") where MySorter loads but is not an ExceptionSorter, is abstract/inaccessible, or its constructor throws.","commonSituations":"Pointing exceptionSorter at a wrong class (e.g. a dialect class instead of an ExceptionSorter); a custom sorter with a non-public/no no-arg constructor; constructor depends on missing config.","solutions":["Ensure the class implements com.alibaba.druid.pool.ExceptionSorter (or extend NullExceptionSorter/valid existing impls).","Give it a public no-arg constructor and make the class public/concrete.","Read SQLException.getCause() to distinguish ClassCastException from InstantiationException.","Consider letting Druid auto-detect the exceptionSorter from the JDBC URL instead of setting it manually."],"exampleFix":"// before\n// ds.setExceptionSorter(\"com.example.WrongClass\");\n\n// after\n// ds.setExceptionSorter(\"com.alibaba.druid.pool.vendor.MySQLExceptionSorter\");","handlingStrategy":"validation","validationCode":"Class<?> c = Utils.loadClass(exceptionSorterFqn);\nif (c == null) throw new IllegalStateException(\"exceptionSorter class not found: \" + exceptionSorterFqn);\nif (!com.alibaba.druid.pool.ExceptionSorter.class.isAssignableFrom(c))\n    throw new IllegalStateException(exceptionSorterFqn + \" is not an ExceptionSorter\");\ncom.alibaba.druid.pool.ExceptionSorter probe = (com.alibaba.druid.pool.ExceptionSorter) c.newInstance();","typeGuard":"static boolean isValidExceptionSorter(String fqn) {\n    try {\n        Class<?> c = Class.forName(fqn);\n        return com.alibaba.druid.pool.ExceptionSorter.class.isAssignableFrom(c)\n            && !java.lang.reflect.Modifier.isAbstract(c.getModifiers());\n    } catch (Throwable t) { return false; }\n}","tryCatchPattern":"try {\n    ds.setExceptionSorter(fqn);\n} catch (SQLException e) {\n    if (e.getMessage().equals(\"create exceptionSorter error\")) {\n        Throwable cause = e.getCause(); // CCE / IE / constructor-thrown\n        // ensure class implements ExceptionSorter, has public no-arg ctor, ctor succeeds\n    }\n    throw e;\n}","preventionTips":["Prefer letting Druid auto-detect the exceptionSorter from the JDBC URL/DB type.","For custom sorters, implement ExceptionSorter with a public no-arg ctor.","Unit-test reflective instantiation before wiring."],"tags":["datasource","exception-sorter","reflection","configuration"],"backgroundTag":null,"analyzedSha":"fa8dc9912637a2f729eef9f55356621fec18d40e","analyzedAt":"2026-08-14T04:55:06.789Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}