views:

1274

answers:

11
+6  Q: 

C# try-catch-else

One thing that has bugged me with exception handling coming from Python to C# is that in C# there doesn't appear to be any way of specifying an else clause. For example, in Python I could write something like this (Note, this is just an example. I'm not asking what is the best way to read a file):

try
{
    reader = new StreamReader(path);
}
catch (Exception)
{
    // Uh oh something went wrong with opening the file for reading
}
else
{
    string line = reader.ReadLine();
    char character = line[30];
}

From what I have seen in most C# code people would just write the following:

try
{
    reader = new StreamReader(path);
    string line = reader.ReadLine();
    char character = line[30];
}
catch (Exception)
{
    // Uh oh something went wrong, but where?
}

The trouble with this is that I don't want to catch out of range exception coming from the fact that the first line in the file may not contain more than 30 characters. I only want to catch exceptions relating to the reading of the file stream. Is there any similar construct I can use in C# to achieve the same thing?

+29  A: 

Catch a specific class off exceptions

try
{
    reader = new StreamReader(path);
    string line = reader.ReadLine();
    char character = line[30];
}
catch (IOException ex)
{
    // Uh oh something went wrong with I/O
}
catch (Exception ex)
{
    // Uh oh something else went wrong
}

The second catch is optional, of course.

Henk Holterman
I'd rather go for a separate try{...}catch(Exception){...} around the read. With unchecked exceptions, you never know what else the Reader might throw besides IOException -- now or in the future.
Tiberiu Ana
In that case, the Exception will bubble up through the caller and get caught somewhere else. Always handle an Exception when you're ready to handle it.
Adrian Godong
Tiberiu, the "you never know what else" goes with the IndexOutOfRange from char[30] etc. No need to treat the ReadLine different unless there is a good reason. But catching the 'remainder' could happen higher up.
Henk Holterman
The question was specifically how to catch (*all*) exceptions related to reading the file stream (not necessarily limited to IOException). In this case, you can add a try{} around the read, catch all exceptions, then package them up in a ReadingStreamException and throw them again.
Tiberiu Ana
You really, really want to do yourself a favor and _never_ catch+swallow general exceptions outside of general error-logging code. It's bound to lead to difficult to debug bugs once something new gets wrong that you hadn't considered that your existing code deals with incorrectly.
Eamon Nerbonne
+7  A: 

You can do this:

try
{
    reader = new StreamReader(path);
}
catch (Exception)
{
    // Uh oh something went wrong with opening the file for reading
}

string line = reader.ReadLine();
char character = line[30];

But of course, you will have to set reader into a correct state or return out of the method.

Adrian Godong
You beat me to it.
Tundey
This only catches errors in Opening the reader, not in the actual reading .
Henk Holterman
@Henk True, so does the original Python code.
Adrian Godong
Adrian, you are right, but the text says something different.
Henk Holterman
+5  A: 

Exceptions are used differently in .NET - they are for exceptional conditions only.

In fact, you should not catch an exception unless you know what it means, and can actually do something about it.

John Saunders
That advice applies just as strongly to Python as .NET.
Mike Graham
+2  A: 

You can have multiple catch clauses, each specific to the type of exception you wish to catch. So, if you only want to catch IOExceptions, then you could change your catch clause to this:

try
{
    reader = new StreamReader(path);
    string line = reader.ReadLine();
    char character = line[30];
}
catch (IOException)
{    
}

Anything other than an IOException would then propagate up the call stack. If you want to also handle other exceptions, then you can add multiple exception clauses, but you must ensure they are added in most specific to most generic order. For example:

try
{
    reader = new StreamReader(path);
    string line = reader.ReadLine();
    char character = line[30];
}
catch (IOException)
{    
}
catch (Exception)
{
}
Michael McCloskey
+4  A: 

Catch more specific exceptions.

try {
   reader = new StreamReader(path);
   string line = reader.ReadLine();
   char character = line[30];
}
catch(FileNotFoundException e) {
   // thrown by StreamReader constructor
}
catch(DirectoryNotFoundException e) {
   // thrown by StreamReader constructor
}
catch(IOException e) {
   // some other fatal IO error occured
}

Further, in general, handle the most specific exception possible and avoid handling the base System.Exception.

Jason
StreamReader.ReadLine does not throw EndOfStreamException on end of stream. Instead it returns null.
Martin Liversage
@Martin Liversage: You are correct. Thanks.
Jason
+2  A: 

More idiomatically, you would employ the using statement to separate the file-open operation from the work done on the data it contains (and include automatic clean-up on exit)

try {
  using (reader = new StreamReader(path))
  {
    DoSomethingWith(reader);
  }
} 
catch(IOException ex)
{
  // Log ex here
}

It is also best to avoid catching every possible exception -- like the ones telling you that the runtime is about to expire.

Steve Gilham
+1  A: 

You can nest your try statements, too

David Stratton
A: 

Is there any similar construct I can use in C# to acheive the same thing?

No.

Wrap your index accessor with an "if" statement which is the best solution in your case in case of performance and readability.

if (line.length > 30) {
   char character = line [30];
}
Christian Birkl
+1  A: 

You could write it like:

bool success = false;
try {
    reader = new StreamReader(path);
    success = true;
}
catch(Exception) {
    // Uh oh something went wrong with opening the file for reading
}
finally {
    if(success) {
        string line = reader.ReadLine();    
        char character = line[30];
    }
}
Johan Kullbom
A: 

I have taken the liberty to transform your code a bit to demonstrate a few important points.

The using construct is used to open the file. If an exception is thrown you will have to remember to close the file even if you don't catch the exception. This can be done using a try { } catch () { } finally { } construct, but the using directive is much better for this. It guarantees that when the scope of the using block ends the variable created inside will be disposed. For a file it means it will be closed.

By studying the documentation for the StreamReader constructor and ReadLine method you can see which exceptions you may expect to be thrown. You can then catch those you finde appropriate. Note that the documented list of exceptions not always is complete.

// May throw FileNotFoundException, DirectoryNotFoundException,
// IOException and more.
try {
  using (StreamReader streamReader = new StreamReader(path)) {
    try {
      String line;
      // May throw IOException.
      while ((line = streamReader.ReadLine()) != null) {
        // May throw IndexOutOfRangeException.
        Char c = line[30];
        Console.WriteLine(c);
      }
    }
    catch (IOException ex) {
      Console.WriteLine("Error reading file: " + ex.Message);
    }
  }
}
catch (FileNotFoundException ex) {
  Console.WriteLine("File does not exists: " + ex.Message);
}
catch (DirectoryNotFoundException ex) {
  Console.WriteLine("Invalid path: " + ex.Message);
}
catch (IOException ex) {
  Console.WriteLine("Error reading file: " + ex.Message);
}
Martin Liversage
+1  A: 

After seeing the other suggested solutions, here is my approach:

try {
    reader = new StreamReader(path);
}
catch(Exception ex) {
    // Uh oh something went wrong with opening the file stream
    MyOpeningFileStreamException newEx = new MyOpeningFileStreamException();
    newEx.InnerException = ex;
    throw(newEx);
}
    string line = reader.ReadLine();
    char character = line[30];

Of course, doing this makes sense only if you are interested in any exceptions thrown by opening the file stream (as an example here) apart from all other exceptions in the application. At some higher level of the application, you then get to handle your MyOpeningFileStreamException as you see fit.

Because of unchecked exceptions, you can never be 100% certain that catching only IOException out of the entire code block will be enough -- the StreamReader can decide to throw some other type of exception too, now or in the future.

Tiberiu Ana