📄 standardexception.java
字号:
/* Derby - Class org.apache.derby.iapi.error.StandardException Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.iapi.error;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.impl.jdbc.EmbedSQLException;import org.apache.derby.iapi.error.ExceptionSeverity;import org.apache.derby.iapi.services.i18n.MessageService;import org.apache.derby.iapi.services.sanity.SanityManager;import java.sql.SQLException;import java.sql.SQLWarning;/** StandardException is the root of all exceptions that are handled in a standard fashion by the database code, mainly in the language code. <P> This class is abstract to ensure that an implementation only throws a specific exception (e.g. TransactionException) which is a sub-class <P> A method in an iterface in a protocol under com.ibm.db2j.protocol.Database must only throw a StandardException (if it needs to throw an exception). This indicates that the method can throw an exception and therefore its caller must ensure that any resources it allocates will be cleaned up in the event of an exception in the StandardException hierarchy. <P> Implementations of methods that throw StandardException can have throws clause that are more specific than StandardException.*/public class StandardException extends Exception { public static final int REPORT_DEFAULT = 0; public static final int REPORT_NEVER = 1; public static final int REPORT_ALWAYS = 2; /* * Exception State */ private Throwable nestedException; private transient Object[] arguments; private int severity; private String textMessage; private String sqlState; private transient int report; /* ** End of constructors */ protected StandardException(String messageID) { this(messageID, (Throwable) null, (Object[]) null); } protected StandardException(String messageID, Object[] args) { this(messageID, (Throwable) null, args); } protected StandardException(String messageID, Throwable t, Object[] args) { super(messageID); this.severity = getSeverityFromIdentifier(messageID); this.sqlState = getSQLStateFromIdentifier(messageID); this.nestedException = t; this.arguments = args; if (SanityManager.DEBUG) { SanityManager.ASSERT(messageID != null, "StandardException with no messageID"); } } /** * This constructor is used when we already have the * message text. * * @param sqlState the sql state of the message * @param text the text of the message */ private StandardException(String sqlState, String text) { this(sqlState); textMessage = text; } /* ** End of constructors */ /** * Sets the arguments for this exception. */ private final void setArguments(Object[] arguments) { this.arguments = arguments; } /** * Returns the arguments for this exception, * if there are any. */ public final Object[] getArguments() { return arguments; } /** * Sets the nested exception for this exception. */ public final void setNestedException(Throwable nestedException) { this.nestedException = nestedException; } /** * Returns the nested exception for this exception, * if there is one. */ public final Throwable getNestedException() { return nestedException; } /** Yes, report me. Errors that need this method to return false are in the minority. */ public final int report() { return report; } /** Set my report type. */ public final void setReport(int report) { this.report = report; } public final void setSeverity(int severity) { this.severity = severity; } public final int getSeverity() { return severity; } public final int getErrorCode() { return severity; } /** Return the 5 character SQL State. If you need teh identifier that was used to create the message, then use getMessageId(). getMessageId() will return the string that corresponds to the field in org.apache.derby.iapi.reference.SQLState. */ public final String getSQLState() { return sqlState; } /** Convert a message identifer from org.apache.derby.iapi.reference.SQLState to a SQLState five character string. * @param messageID - the sql state id of the message from cloudscape * @return String - the 5 character code of the SQLState ID to returned to the user */ public static String getSQLStateFromIdentifier(String messageID) { if (messageID.length() == 5) return messageID; return messageID.substring(0, 5); } /** Get the severity given a message identifier from org.apache.derby.iapi.reference.SQLState. */ public static int getSeverityFromIdentifier(String messageID) { int lseverity = ExceptionSeverity.NO_APPLICABLE_SEVERITY; switch (messageID.length()) { case 5: switch (messageID.charAt(0)) { case '0': switch (messageID.charAt(1)) { case '1': lseverity = ExceptionSeverity.WARNING_SEVERITY; break; case 'A': case '7': lseverity = ExceptionSeverity.STATEMENT_SEVERITY; break; case '8': lseverity = ExceptionSeverity.SESSION_SEVERITY; break; } break; case '2': case '3': lseverity = ExceptionSeverity.STATEMENT_SEVERITY; break; case '4': switch (messageID.charAt(1)) { case '0': lseverity = ExceptionSeverity.TRANSACTION_SEVERITY; break; case '2': lseverity = ExceptionSeverity.STATEMENT_SEVERITY; break; } break; } break; default: switch (messageID.charAt(6)) { case 'M': lseverity = ExceptionSeverity.SYSTEM_SEVERITY; break; case 'D': lseverity = ExceptionSeverity.DATABASE_SEVERITY; break; case 'C': lseverity = ExceptionSeverity.SESSION_SEVERITY; break; case 'T': lseverity = ExceptionSeverity.TRANSACTION_SEVERITY; break; case 'S': lseverity = ExceptionSeverity.STATEMENT_SEVERITY; break; case 'U': lseverity = ExceptionSeverity.NO_APPLICABLE_SEVERITY; break; } break; } return lseverity; } /* ** Set of static methods to obtain exceptions. ** ** Possible parameters: ** String sqlState - SQL State ** int severity - Severity of message ** Throwable t - exception to wrap ** Object aN - argument to error message ** ** Calls that can be made after the exception has been created. ** ** setExceptionCategory() ** setReport() */ /* specific exceptions */ public static StandardException normalClose() { StandardException se = newException( SQLState.NORMAL_CLOSE ); se.report = REPORT_NEVER; return se; } public static StandardException errorClose( Throwable t ) { StandardException se = newException( SQLState.ERROR_CLOSE, t ); se.report = REPORT_NEVER; return se; } /* 0 arguments */ public static StandardException newException(String messageID) { return new StandardException(messageID); } public static StandardException newException(String messageID, Throwable t) { return new StandardException(messageID, t, (Object[]) null); } /* 1 argument */ public static StandardException newException(String messageID, Object a1) { Object[] oa = new Object[] {a1}; return new StandardException(messageID, oa); } public static StandardException newException(String messageID, Throwable t, Object a1) { Object[] oa = new Object[] {a1}; return new StandardException(messageID, t, oa); } /* 2 arguments */ public static StandardException newException(String messageID, Object a1, Object a2) { Object[] oa = new Object[] {a1, a2}; return new StandardException(messageID, oa); } public static StandardException newException(String messageID, Throwable t, Object a1, Object a2) { Object[] oa = new Object[] {a1, a2}; return new StandardException(messageID, t, oa); } /* 3 arguments */ public static StandardException newException(String messageID, Object a1, Object a2, Object a3) { Object[] oa = new Object[] {a1, a2, a3}; return new StandardException(messageID, oa); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -