6 Replies - 404 Views - Last Post: 03 November 2017 - 01:23 AM Rate Topic: -----

#1 garbus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-June 17

Proper Way to Handle, Name, and Throw Exceptions

Posted 23 October 2017 - 05:43 AM

When writing a method, either in an application or library, or some other type of project, how do you know which exceptions to handle? Should you handle every exception that might be thrown within your method? Also, how do you decide when to throw your own exceptions from a method, and how do you decide what to name them?
Is This A Good Question/Topic? 0
  • +

Replies To: Proper Way to Handle, Name, and Throw Exceptions

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,102
  • Joined: 05-May 12

Re: Proper Way to Handle, Name, and Throw Exceptions

Posted 23 October 2017 - 10:30 AM

You only handle exceptions that you know how to recover from. Any other exception should be allowed to bubble up.

Exceptions should be thrown to indicate exceptional conditions. Anything else should be dealt with by using error codes. Exceptions should not be used for flow control.

Always prefer to use built in exceptions before opting to create a new exception. Exception names should be as descriptive as possible to let developers have a quick idea of what went wrong before being forced to read the exception message text.
Was This Post Helpful? 1
  • +
  • -

#3 aidenkael  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 59
  • View blog
  • Posts: 275
  • Joined: 22-October 13

Re: Proper Way to Handle, Name, and Throw Exceptions

Posted 02 November 2017 - 10:50 AM

When I write code, I think "what stupid think could go wrong?" Take the following code:

public int AddMyNumbers(int a, int B)/>/>/>/>
{
    return a + b;
}



Simple right? Now what could go wrong? Null values for a and b, how about a stackoverflow? I then might do something like
public int AddMyNumbers(int a, int B)/>/>/>/>
{
    if(a == null) throw new ArgumentNullException(nameof(a));
    if(b == null) throw new ArgumentNullException(nameof(B)/>/>/>);

    return a + b;
}



I let the caller function decide how to handle these exceptions.

For when to handle exceptions, I only handle exceptions I can recover from. I let anything I can't recover from bubble up because these generally show a design flaw.

EDIT: Not sure why the /> tags are happening.......

This post has been edited by aidenkael: 02 November 2017 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,102
  • Joined: 05-May 12

Re: Proper Way to Handle, Name, and Throw Exceptions

Posted 02 November 2017 - 11:06 AM

int cannot be null.

Very likely you forgot to instantiate the class that holds your AddNumbers() method and therefore getting the null reference exception.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,102
  • Joined: 05-May 12

Re: Proper Way to Handle, Name, and Throw Exceptions

Posted 02 November 2017 - 11:14 AM

The forum software has a bug when dealing with some text where it thinks it is an emoticon that needs to be escaped.
Was This Post Helpful? 0
  • +
  • -

#6 aidenkael  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 59
  • View blog
  • Posts: 275
  • Joined: 22-October 13

Re: Proper Way to Handle, Name, and Throw Exceptions

Posted 02 November 2017 - 09:51 PM

View PostSkydiver, on 02 November 2017 - 01:06 PM, said:

int cannot be null.


Very true and a good call out. This isn't actual code I have, just wrote it inside the code tags to give OP a visual of what we were saying. For compilation purposes, you can change the
 int 
to whatever nullable type you wish :)
Was This Post Helpful? 0
  • +
  • -

#7 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • Joined: 26-September 16

Re: Proper Way to Handle, Name, and Throw Exceptions

Posted 03 November 2017 - 01:23 AM

If I were writing a library, I would probably add code that checked for exceptions only in the public methods that would make sure that the private methods called from within them got correct arguments. This makes sense especially when a private method has to be called repeatedly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1