# Refactoring 004 - Remove Unhandled Exceptions

> TL;DR: Remove unnecessary and not references empty exception classes.

# Problems Addressed

- Empty Classes

- Namespace Polluted

# Related Code Smells

- %[https://maximilianocontieri.com/code-smell-26-exceptions-polluting]

# Steps

1. Check there are no references to the empty exception class.

2. Replace the throw sentence with a generic one.

# Sample Code

## Before

[Gist Url]: # (https://gist.github.com/mcsee/c95a843d906b0e339ec617779f79f538)
```ruby
class RangeNotSatisfiedException < StandardError
end

begin
    raise RangeNotSatisfiedException.new "Range must be betweet 0 and 10"
rescue RangeNotSatisfiedException => e
    puts e.message 
    puts e.exception_type 
end


```

## After

[Gist Url]: # (https://gist.github.com/mcsee/63915cf29a543ce091267619bb21917b)
```ruby
# 1. Check there are no references to the empty exception class.

# 2. Replace the throw sentence with a generic one.

begin
    raise StandardError.new "Range must be betweet 0 and 10"
rescue StandardError => exception
    puts exception.message 
    puts exception.exception_type 
end
```

# Type

[X] Automatic

If the Exception class has no references we can perform a Safe Remove and replace it with Exception class.

# Why the code is better?

- We remove an empty class nobody uses. 

- We shrink the code

# Limitations

If we need to declare an empty exception class as documentation for an API module, our clients might need to catch it.

This is a [gold plating](https://en.wikipedia.org/wiki/Gold_plating_(project_management) and [YAGNI](https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it) example.

# Tags

- Clean up

# Related Refactorings

- Safe Remove

# Credits

Image by <a href="https://pixabay.com/es/users/danielkirsch-4218687/">danielkirsch</a> from <a href="https://pixabay.com/">Pixabay</a>

* * * 

This article is part of the Refactoring Series.
