The OracleException class represents an exception that is thrown when the Oracle Data Provider for .NET encounters an error. Each OracleException object contains at least one OracleError object in the Error property that describes the error or warning.
System.Object
System.Exception
System.SystemException
System.Runtime.InteropServices.ExternalException
System.Data.Common.DbException
Oracle.DataAccess.Client.OracleException
// C# public sealed class OracleException : SystemException
| Provider | ODP.NET, Unmanaged Driver | ODP.NET, Managed Driver |
| Assembly | Oracle.DataAccess.dll |
Oracle.ManagedDataAccess.dll |
| Namespace | Oracle.DataAccess.Client |
Oracle.ManagedDataAccess.Client |
| .NET Framework | 3.5, 4.0, 4.5, 4.6 | 4.0, 4.5, 4.6 |
All public static methods are thread-safe, although instance methods do not guarantee thread safety.
If there are multiple errors, ODP.NET only returns the first error message on the stack.
// C#
using System;
using System.Data;
using Oracle.DataAccess.Client;
class OracleExceptionSample
{
static void Main()
{
string constr = "User Id=scott;Password=tiger;Data Source=oracle";
OracleConnection con = new OracleConnection(constr);
con.Open();
// Create an OracleCommand object using the connection object
OracleCommand cmd = con.CreateCommand();
try
{
cmd.CommandText = "insert into notable values (99, 'MyText')";
cmd.ExecuteNonQuery();
}
catch (OracleException ex)
{
Console.WriteLine("Record is not inserted into the database table.");
Console.WriteLine("Exception Message: " + ex.Message);
Console.WriteLine("Exception Source: " + ex.Source);
}
}
}
See Also:
OracleException members are listed in the following tables.
OracleException Static Methods
The OracleException static method is listed in Table 6-77.
Table 6-77 OracleException Static Method
| Method | Description |
|---|---|
|
|
Inherited from |
OracleException properties are listed in Table 6-78.
Table 6-78 OracleException Properties
| Property | Description |
|---|---|
|
Specifies the TNS name that contains the information for connecting to an Oracle instance |
|
|
Specifies a collection of one or more |
|
|
|
Inherited from |
|
|
Inherited from |
|
Specifies whether the current operation producing this exception can succeed if retried |
|
|
Specifies the error messages that occur in the exception |
|
|
Specifies the Oracle error number |
|
|
Returns an |
|
|
Specifies the stored procedure that cause the exception |
|
|
Specifies the name of the data provider that generates the error |
|
|
|
Inherited from |
|
|
Inherited from |
OracleException methods are listed in Table 6-79.
Table 6-79 OracleException Methods
| Method | Description |
|---|---|
|
|
Inherited from |
|
|
Inherited from |
|
|
Inherited from |
|
Sets the serializable |
|
|
|
Inherited from |
|
Returns the fully qualified name of this exception |
The OracleException static method is listed in Table 6-80.
OracleException properties are listed in Table 6-81.
Table 6-81 OracleException Properties
| Property | Description |
|---|---|
|
Specifies the TNS name that contains the information for connecting to an Oracle instance |
|
|
Specifies a collection of one or more |
|
|
|
Inherited from |
|
|
Inherited from |
|
Specifies whether the current operation producing this exception can succeed if retried |
|
|
Specifies the error messages that occur in the exception |
|
|
Specifies the Oracle error number |
|
|
Returns an |
|
|
Specifies the stored procedure that cause the exception |
|
|
Specifies the name of the data provider that generates the error |
|
|
|
Inherited from |
|
|
Inherited from |
See Also:
This property specifies the TNS name that contains the information for connecting to an Oracle instance.
// C#
public string DataSource {get;}
The TNS name containing the connect information.
This property specifies a collection of one or more OracleError objects that contain information about exceptions generated by the Oracle database.
// C#
public OracleErrorCollection Errors {get;}
An OracleErrorCollection.
The Errors property contains at least one instance of OracleError objects.
This property specifies whether the current operation producing this exception can succeed if retried.
// C#
public bool IsRecoverable {get;}
A bool.
When a database outage occurs, such as during a network failure, the session becomes unavailable and the client receives an error code. The client can have difficulty determining whether the in-flight operation committed or needs to be resubmitted. Oracle automatically determines whether an in-flight database operation can be recovered or not using the IsRecoverable property. If IsRecoverable returns true after an outage, then the application can retrieve the current operation status and complete the transaction. If IsRecoverable returns false, then the application can rollback the current operation and resubmit the transaction.
This property is often used in conjunction with Transaction Guard.
Overrides Exception
This property specifies the error messages that occur in the exception.
// C#
public override string Message {get;}
A string.
Message is a concatenation of all errors in the Errors collection. Each error message is concatenated and is followed by a carriage return, except the last one.
This property specifies the Oracle error number.
// C#
public int Number {get;}
The error number.
This error number can be the topmost level of error generated by Oracle and can be a provider-specific error number.
This property will returns an OracleLogicalTransaction object for a recoverable error when using Transaction Guard.
// C#
public OracleLogicalTransaction OracleLogicalTransaction {get;}
An OracleLogicalTransaction.
OracleLogicalTransaction is non-null when both of the following conditions are met:
Transaction Guard is enabled on the service
The exception is a recoverable error
OracleLogicalTransaction can be used to determine the transaction outcome by looking at the two properties that it exposes: Committed and UserCallCompleted. If the outcome is not known, then Committed and UserCallCompleted will be set to null.
If the outcome of a recoverable error could not be determined by ODP.NET and the connection have not participated in a distributed transaction, then the OracleLogicalTransactionId property of the OracleLogicalTransaction object will be non-null and it can be used to determine the outcome by having the application explicitly call the OracleLogicalTransaction.GetOutcome static method, if the database/service is up.
This property specifies the stored procedure that caused the exception.
// C#
public string Procedure {get;}
The stored procedure name.
OracleException methods are listed in Table 6-82.
Table 6-82 OracleException Methods
| Method | Description |
|---|---|
|
|
Inherited from |
|
|
Inherited from |
|
|
Inherited from System. |
|
Sets the serializable |
|
|
|
Inherited from |
|
Returns the fully qualified name of this exception |
See Also:
Overrides Exception
This method sets the serializable info object with information about the exception.
// C# public override void GetObjectData(SerializationInfo info, StreamingContext context);
info
A SerializationInfo object.
context
A StreamingContext object.
The information includes DataSource, Message, Number, Procedure, Source, and StackTrace.
Overrides Exception
This method returns the fully qualified name of this exception, the error message in the Message property, the InnerException.ToString() message, and the stack trace.
// C# public override string ToString();
The string representation of the exception.
// C#
using System;
using Oracle.DataAccess.Client;
class ToStringSample
{
static void Main()
{
string constr = "User Id=scott;Password=tiger;Data Source=oracle";
OracleConnection con = new OracleConnection(constr);
con.Open();
// Create an OracleCommand object using the connection object
OracleCommand cmd = con.CreateCommand();
try
{
cmd.CommandText = "insert into notable values (99, 'MyText')";
cmd.ExecuteNonQuery(); // This will throw an exception
}
catch (OracleException ex)
{
Console.WriteLine("Record is not inserted into the database table.");
Console.WriteLine("ex.ToString() : " + ex.ToString());
}
}
}