Try-catch block

I have a class with different methods doing the same thing while reading a file. I want to use a try-catch block to handle exceptions. I want to ask if there is a way for all methods to get inside the same try block, since each method will give the same exception “file not found”.

+5
source share
7 answers

My preferred processing method is to call a common method for all of them, so each (individually) looks like this:

try {
   // code
} catch(SomeExceptionType ex) {
   DoSomethingAboutThat(ex);
}

However, you can also do this with delegates, i.e.

void Execute(Action action) {
    try {
       // code
    } catch(SomeExceptionType ex) {
       // do something
    }
}

and

Execute(() => {open file});
+3
source

:

public static class ActionExtensions
{
    public static Action WrapWithMyCustomHandling(this Action action)
    {
        return () =>
                {
                    try
                    {
                        action();
                    }
                    catch (Exception exception)
                    {

                        // do what you need 
                    }
                };
    }
}

public class DummyClass
{
    public void DummyMethod()
    {
        throw new Exception();
    }
}

, :

DummyClass dummyClass = new DummyClass();
Action a = () => dummyClass.DummyMethod();
a.WrapWithMyCustomHandling()();

, .

+3

, try catch, catch, :

try
{
    .... your code
}
catch (SomeException e)
{
    ExceptionHandler(e);
}
+2

try - catch , , , try - catch .

( , .)

+1

, " , ", . ?

, catch try , . , - .

0

, , , , . , FileNotFoundException , . filestream .

:

 private bool PerformActionOnFileStream(Action<FileStream> action, string path)
 {
      try
      {
           using(FileStream fileStream = new FileStream(@path, FileMode.Open))               
           {
                action(fileStream);
           }
      }
      catch(FileNotFoundException)
      {
           return false;
      }
 }

:

 private void PrintContentOfFile(string path)
 {
      Action<FileStream> action = fileStream => PrintContentOfFileStream(fileStream);

      bool didPerformAction = PerformActionOnFileStream(action, path);
      if(!didPerformAction)
      {
           // Handle error.
      }
 }
0

Aliostad , , , , , , . , , , , , , .

, , , , Try-Catch, , .

, :

public void HandleError(Sysem.Reflection.MethodBase MethodBase, Exception Exception);

, - , .

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>try</Title>
        <Shortcut>try</Shortcut>
        <Description>Code snippet for try catch</Description>
        <Author>Microsoft Corporation</Author>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
            <SnippetType>SurroundsWith</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>expression</ID>
                <ToolTip>Exception type</ToolTip>
                <Function>SimpleTypeName(global::System.Exception)</Function>
            </Literal>
        </Declarations>
        <Code Language="csharp">
<![CDATA[try 
{           
    $selected$
}
catch (Exception ex)
{
        YourErrorHandler.HandleError(System.Reflection.MethodBase.GetCurrentMethod(), ex);
}]]>
        </Code>
    </Snippet>
</CodeSnippet>

, HandleError , , , , , , .

!

0

All Articles