Skip to content

Instantly share code, notes, and snippets.

@jamesmanning
Created April 19, 2013 19:03
Show Gist options
  • Save jamesmanning/5422449 to your computer and use it in GitHub Desktop.
Save jamesmanning/5422449 to your computer and use it in GitHub Desktop.
using exception.ToString - sync code
using System;
using System.IO;
using System.Threading;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
string filePath = null; // should have been set to something, but wasn't
try
{
var result = ReadData(filePath);
Console.WriteLine("Result is {0}", result);
}
catch (Exception ex)
{
Console.WriteLine("Exception happened - Message = {0}
", ex.Message);
Console.WriteLine("Exception happened - StackTrace = {0}
", ex.StackTrace);
Console.WriteLine("Exception happened - ToString() = {0}
", ex);
}
}
public static int ReadData(string filePath)
{
try
{
// do other stuff
Thread.Sleep(1000);
var readDataImpl = ReadDataImpl(filePath);
return readDataImpl;
}
catch (Exception ex)
{
throw new MyException("I just don't know what went wrong!", ex);
}
}
public static int ReadDataImpl(string filePath)
{
try
{
var readAllBytes = File.ReadAllBytes(filePath);
var length = readAllBytes.Length;
return length;
}
catch (Exception ex)
{
throw new MyException("Exception during File.ReadAllBytes - My bad!", ex);
}
}
public class MyException : Exception
{
public MyException(string message, Exception innerException) : base(message, innerException) { }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment