简体   繁体   中英

What way is better to use exceptions?

I have two ways for create exception.

The first way: I use base Exception class for create exception.

public class Class1
{
    public void Method1()
    {
        try
        {
            Method1ThrowException();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

    public void Method1ThrowException()
    {
        throw new Exception();
    }
}

The second way: In this way I create User-Defined Exception class and then use this class for create exception.

public class Class2
{
    public void Method2()
    {
        try
        {
            Method2ThrowException();
        }
        catch (MethodException e)
        {
            Console.WriteLine(e);
        }
    }

    public void Method2ThrowException()
    {
        throw new MethodException();
    }
}

public class MethodException : Exception
{
    public MethodException()
    {

    }

    public MethodException(string message) : base(message)
    {

    }

    public MethodException(string message, Exception inner) : base(message, inner)
    {

    }
}

What the difference between this ways use of exception?

I read that use the second way is better and I don't understand why? In both of ways I just create exceptions and get them.

The second way is better because you create your own exception type which can be suitable for specific situation. For example: you could create ConnectionBrokenException type when writing application which communicates over some network and throw it when connection is broken.

This gives you two main advantages:

First
You can handle exceptions in different ways depending on the exception type using try..catch block. You may decide to show message to user, rollback some operations or do other stuff.

try
{
    ... code throwing ConnectionBrokenException
}
catch(ConnectionBrokenException ex)
{
    _logger.LogError("Something bad has happened with connection", ex.Message);
}
catch(Exception)
{
    // handles other types of exceptions
}

Second
When analyzing logs from such application it is easier to diagnose some problems when each bad situation has its own exception. You can for example use some filtering to find only exception you're interested in.

Because catch (Exception e) catches everything . There is no way to discriminate what kind of exception was raised. Thus, fine-grained error classes are good.

(Also, why define Method1ThrowException , instead of just throw ing the exception where it needs to be thrown? This just adds a useless layer to the stacktrace.)

There is no 'better' way, as it depends on your specific needs.

If there is an existing exception type you can use, just use that one ( ArgumentException etc). See here for a list.

If you need special exception type, create your own, containing your own custom properties. Example:

 public BusinessException : Exception
 {
     public BusinessException(string reason)
     {
           Reason  = reason;
     }
     public string Reason {get; set;}
 }

If you are throwing these in a WCF service, be sure to add a global exception handler to map these exceptions to WCF faults. In case of web API, map them to HTML status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM