OracleException Class

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.

Class Inheritance

System.Object

  System.Exception

    System.SystemException

      System.Runtime.InteropServices.ExternalException

        System.Data.Common.DbException

         Oracle.DataAccess.Client.OracleException

Declaration

// C#
public sealed class OracleException : SystemException

Requirements

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.0, 4.5

Thread Safety

All public static methods are thread-safe, although instance methods do not guarantee thread safety.

Remarks

If there are multiple errors, ODP.NET only returns the first error message on the stack.

Example

// 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);
    }
  }
}

OracleException Members

OracleException members are listed in the following tables.

OracleException Static Methods

The OracleException static method is listed in Table 6-75.

Table 6-75 OracleException Static Method

Method Description

Equals

Inherited from System.Object (Overloaded)


OracleException Properties

OracleException properties are listed in Table 6-76.

Table 6-76 OracleException Properties

Property Description

DataSource

Specifies the TNS name that contains the information for connecting to an Oracle instance

Errors

Specifies a collection of one or more OracleError objects that contain information about exceptions generated by the Oracle database

HelpLink

Inherited from System.Exception

InnerException

Inherited from System.Exception

IsRecoverable

Specifies whether the current operation producing this exception can succeed if retried

Not available in the ODP.NET, Managed Driver

Message

Specifies the error messages that occur in the exception

Number

Specifies the Oracle error number

Procedure

Specifies the stored procedure that cause the exception

Source

Specifies the name of the data provider that generates the error

StackTrace

Inherited from System.Exception

TargetSite

Inherited from System.Exception


OracleException Methods

OracleException methods are listed in Table 6-77.

Table 6-77 OracleException Methods

Method Description

Equals

Inherited from System.Object (Overloaded)

GetBaseException

Inherited from System.Exception

GetHashCode

Inherited from System.Object

GetObjectData

Sets the serializable info object with information about the exception

GetType

Inherited from System.Object

ToString

Returns the fully qualified name of this exception


OracleException Static Methods

The OracleException static method is listed in Table 6-78.

Table 6-78 OracleException Static Method

Method Description

Equals

Inherited from System.Object (Overloaded)


OracleException Properties

OracleException properties are listed in Table 6-79.

Table 6-79 OracleException Properties

Property Description

DataSource

Specifies the TNS name that contains the information for connecting to an Oracle instance

Errors

Specifies a collection of one or more OracleError objects that contain information about exceptions generated by the Oracle database

HelpLink

Inherited from System.Exception

InnerException

Inherited from System.Exception

IsRecoverable

Specifies whether the current operation producing this exception can succeed if retried

Not available in the ODP.NET, Managed Driver

Message

Specifies the error messages that occur in the exception

Number

Specifies the Oracle error number

Procedure

Specifies the stored procedure that cause the exception

Source

Specifies the name of the data provider that generates the error

StackTrace

Inherited from System.Exception

TargetSite

Inherited from System.Exception


DataSource

This property specifies the TNS name that contains the information for connecting to an Oracle instance.

Declaration

// C#
public string DataSource {get;}

Property Value

The TNS name containing the connect information.

Errors

This property specifies a collection of one or more OracleError objects that contain information about exceptions generated by the Oracle database.

Declaration

// C#
public OracleErrorCollection Errors {get;}

Property Value

An OracleErrorCollection.

Remarks

The Errors property contains at least one instance of OracleError objects.

IsRecoverable

This property specifies whether the current operation producing this exception can succeed if retried.

Declaration

// C#
public bool IsRecoverable {get;}

Property Value

A bool.

Remarks

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.

Message

Overrides Exception

This property specifies the error messages that occur in the exception.

Declaration

// C#
public override string Message {get;}

Property Value

A string.

Remarks

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.

Number

This property specifies the Oracle error number.

Declaration

// C#
public int Number {get;}

Property Value

The error number.

Remarks

This error number can be the topmost level of error generated by Oracle and can be a provider-specific error number.

Procedure

This property specifies the stored procedure that caused the exception.

Declaration

// C#
public string Procedure {get;}

Property Value

The stored procedure name.

Source

Overrides Exception

This property specifies the name of the data provider that generates the error.

Declaration

// C#
public override string Source {get;}

Property Value

The name of the data provider.

OracleException Methods

OracleException methods are listed in Table 6-80.

Table 6-80 OracleException Methods

Method Description

Equals

Inherited from System.Object (Overloaded)

GetBaseException

Inherited from System.Exception

GetHashCode

Inherited from System.Object

GetObjectData

Sets the serializable info object with information about the exception

GetType

Inherited from System.Object

ToString

Returns the fully qualified name of this exception


GetObjectData

Overrides Exception

This method sets the serializable info object with information about the exception.

Declaration

// C#
public override void GetObjectData(SerializationInfo info, StreamingContext 
    context);

Parameters

  • info

    A SerializationInfo object.

  • context

    A StreamingContext object.

Remarks

The information includes DataSource, Message, Number, Procedure, Source, and StackTrace.

ToString

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.

Declaration

// C#
public override string ToString();

Return Value

The string representation of the exception.

Example

// 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());
    }
  }
}