You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
.NET
Exception Handling (.NET SDK)
Copy page
Download PDF
Exception Handling (.NET SDK)
This document describes exception handling when using the TOS SDK.​
TOS Server-Side Error​
The TOS server complies with HTTP standards and specifies detailed HTTP status codes and business error codes for scenarios involving request failures. For detailed information, please refer to Definition of TOS Server Status Codes and Error Codes.​
SDK Custom Exceptions​
Client Error - TosClientError​
TosClientError is caused by client input parameter errors, connection timeouts, or special client operations such as canceling resumable upload processes and other similar special operations. For example, when using CreateBucket to create a bucket, an exception is returned if the bucket name exceeds 63 characters in length.​
Server-Side Exception - TosServerError​
Requests are sent to the TOS server via the TOS .NET SDK interface. The response to each request includes status codes that comply with HTTP standards, as well as business error codes, which together indicate the response status of the current request.​
Exception Handling Examples​
The following code demonstrates exception handling when attempting to download a non-existent object. If a TosServerError exception occurs, the exception message, HTTP request status code, and request ID are printed. If a TosClientError exception occurs, the exception message and the underlying cause of the error are printed.​
​
using System;​
using System.IO;​
using TOS;​
using TOS.Error;​
using TOS.Model;​
​
namespace ConsoleApp​
{​
internal class Program​
{​
private static void Main(string[] args)​
{​
var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY");​
var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY");​
var endpoint = "https://tos-cn-beijing.volces.com";​
var region = "cn-beijing";​
var bucketName = "*** Provide your bucket name ***";​
var objectKey = "example_dir/example.txt";​
​
var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();​
​
try​
{​
var getObjectInput = new GetObjectInput()​
{​
Bucket = bucketName,​
Key = objectKey,​
};​
var getObjectOutput = client.GetObject(getObjectInput);​
Console.WriteLine("GetObject succeeded, request id {0}", getObjectOutput.RequestID);​
Console.WriteLine("GetObject succeeded, content length {0}", getObjectOutput.ContentLength);​
using (var reader = new StreamReader(getObjectOutput.Content))​
{​
Console.WriteLine("GetObject succeeded, content {0}", reader.ReadToEnd());​
getObjectOutput.Content.Close();​
}​
}​
catch (TosServerException ex)​
{​
if (ex.StatusCode == 404)​
{​
Console.WriteLine("GetObject failed, object not exist");​
}​
​
Console.WriteLine("GetObject failed, request id {0}", ex.RequestID);​
Console.WriteLine("GetObject failed, status code {0}", ex.StatusCode);​
Console.WriteLine("GetObject failed, response error code {0}", ex.Code);​
Console.WriteLine("GetObject failed, response error message {0}", ex.Message);​
}​
catch (TosClientException ex)​
{​
Console.WriteLine("GetObject failed, error message {0}", ex.Message);​
}​
catch (Exception ex)​
{​
Console.WriteLine("GetObject failed, {0}", ex.Message);​
}​
}​
}​
}​
​
​
Last updated: 2025.09.23 11:28:04