Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Exception Handling Fall Through [duplicate]

Possible Duplicate:
Catch multiple Exceptions at once?

Is there any way in C# to easily achieve the following pseduo-code:

try
{
...
}
catch ( ExceptionTypeA, ExceptionTypeB, ExceptionTypeC as ex)
{
... same code for all threw
}

Or

try
{
...
}
catch ( ExceptionTypeA ex )
catch ( ExceptionTypeB ex )
catch ( ExceptionTypeC ex )
{
... same code for all exceptions of A, B or C
}

I guess what I'm saying would be great would be fall-through on exception types.

like image 705
Keith Adler Avatar asked Nov 18 '09 21:11

Keith Adler


People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

C dalam Latin berapa?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).

Bahasa C dibuat pertama kali oleh siapa dan tahun berapa?

Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.


5 Answers

The problem with the syntax as mentioned (with an ex) is: what properties/members should ex have? Different exception types are not necessarily compatible, unless there is inheritance involved (in which case, catch the least-derived that you care about).

The only current option is to just use Exception ex (or similar) and check (is/as) inside the handler.

Or; refactor the common code into a method that can be used by all three?

like image 84
Marc Gravell Avatar answered Oct 22 '22 08:10

Marc Gravell


In short, no. I can think of two three alternatives:

Catch each exception, and call a common method:

try
{
   // throw
}
catch ( ExceptionTypeA ex )
{
     HandleException();
}
catch ( ExceptionTypeB ex )
{
     HandleException();
}
catch ( ExceptionTypeC ex )
{
     HandleException();
}

void HandleException()
{
}

Or catch everything, and use an if statement on the type:

try
{
   // throw
}
catch (Exception ex)
{
   if (ex is ArgumentException || ex is NullReferenceException || ex is FooException)
   {
      // Handle
   }
   else
   {
      throw
   }
}

EDIT: OR, you could do something like this:

List<Type> exceptionsToHandle = new List<Type>{ typeof(ArgumentException), typeof(NullReferenceException), typeof(FooException) };

try
{
   // throw
}
catch (Exception ex)
{
   if (exceptionsToHandle.Contains(ex.GetType()))
   {
      // Handle
   }
   else
   {
      throw
   }
}
like image 39
Philip Wallace Avatar answered Oct 22 '22 10:10

Philip Wallace


You can catch a general exception and then examine the type, e.g.:

catch (Exception ex)            
   {                
      if (ex is ExceptionTypeA ||
          ex is ExceptionTypeB )
           {
               /* your code here */
           }
       else
           {
               throw;
           }
    }

Edit: in line with other answers I'd be looking to clarify what's going on by pulling out a method - but rather than individual catches and a common method, I'd probably introduce a method to clarify what the contents of the if statement is doing. So instead of

if (ex is ExceptionTypeA || ex is ExceptionTypeB )

it'd become something like:

if (IsRecoverableByDoingWhatever(ex))

which I think would clarify the intent more than pulling out the handler code (although that might be useful to do too).

like image 39
FinnNk Avatar answered Oct 22 '22 09:10

FinnNk


Wrap the repetitive code in a method.

try
{
...
}
catch ( ExceptionTypeA ex )
{
     DoSomething();
}
catch ( ExceptionTypeB ex )
{
     DoSomething();
}
catch ( ExceptionTypeC ex )
{
     DoSomething();
}
catch ( Exception ex )
{
     DoTheDefaultSomething();
}
like image 34
Clark Avatar answered Oct 22 '22 08:10

Clark


If you need to use some variables from the scope of try, use a nested function. That is, a lambda or an anonymous delegate:

int x = ...;
Action<Exception> handler = delegate(Exception ex)
{
    // Same code for all exceptions of A, B or C.
    // You can use variable x here too.
};    

try
{
...
}
catch (ExceptionTypeA ex) { handler(ex); }
catch (ExceptionTypeB ex) { handler(ex); }
catch (ExceptionTypeC ex) { handler(ex); }
like image 38
Pavel Minaev Avatar answered Oct 22 '22 09:10

Pavel Minaev