# \[code\] Example of creating your own Exception subclasses

**URL:** <https://forums.sketchup.com/t/code-example-of-creating-your-own-exception-subclasses/36906>\
**Category:** Ruby API\
**Tags:** exception, inputbox, input, example\
**Created:** [January 6, 2017, 6:49pm UTC](https://forums.sketchup.com/t/code-example-of-creating-your-own-exception-subclasses/36906 "2017-01-06T18:49:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [January 6, 2017, 6:49pm UTC](https://forums.sketchup.com/t/code-example-of-creating-your-own-exception-subclasses/36906/1 "2017-01-06T18:49:12Z")

</div>

Prompted by a question ([over at SCF](http://sketchucation.com/forums/viewtopic.php?f=180&t=66647),) about using a **`SystemExit`** exception (fired by **`exit()`**,) to bail out of an input handling routine.

The main answer is: I know of **no** good use for **`exit()`** in an embedded Ruby subprocess.

… and a better solution is to use custom exception subclasses (_usually_ of **`RuntimeError`**.)

[Custom\_Exception\_SubClasses.rb](https://forums.sketchup.com/uploads/short-url/mQbAuaLAzPdxwL26pwnuS1HU8OF.rb) (1.8 KB)

```ruby
# encoding : UTF-8

# A simple example of creating your own Exception
# sub-classes inside your own namespace modules.
#
# fail() is an alias for raise(), and most coding style guides
# prefer that fail() is used when first causing an exception,
# and raise() is used when re-raising a current exception within
# a rescue block.

module Author # <--<<< Change to unique toplevel namespace module.

  module SomeParticularPlugin

    VERSION = '1.0.0'
    
    InputCancellation = Class.new(RuntimeError)
    InvalidSelection = Class.new(RuntimeError)
    NoGroupsSelected = Class.new(RuntimeError)

    # In this module we assume that the module variables: @@prompts,
    # @@defaults, @@choices & @@caption have already been defined.

    def get_input(selection_set)
      
      # First, make a decision about the selection set:
      if selection_set.empty?
        fail(InvalidSelection,"selection is empty!")
      elsif !selection_set.single_object?
        fail(InvalidSelection,"selection not single object!")
      else
        grps = selection_set.grep(Sketchup::Group)
        if grps.empty?
          fail(NoGroupsSelected,"no groups are selected!")
        end
      end

      data = inputbox(@@prompts,@@defaults,@@choices,@@caption)
      # false if the user cancels
      fail(InputCancellation,"user cancelled") if !data
      # otherwise data is an array of values

    rescue InputCancellation => err
      UI.messagebox("Stop: #{err.message}")
      return nil
    rescue InvalidSelection => err
      UI.messagebox("Selection Error: #{err.message}")
      return false
    rescue => err # any other exception
      # recover gracefully
      UI.messagebox("Error: #{err.message}")
      return false
    else # no errors
      # code to execute when input is valid
      return data
    end ###

  end # specific plugin module
end # outermost namespace module

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [January 7, 2017, 5:14pm UTC](https://forums.sketchup.com/t/code-example-of-creating-your-own-exception-subclasses/36906/2 "2017-01-07T17:14:12Z")

</div>

I just noticed an error, although the example should still work.

Line 38 was meant to be:

```ruby
      data = UI.inputbox(@@prompts,@@defaults,@@choices,@@caption)

```

Currently it is:

```ruby
      data = inputbox(@@prompts,@@defaults,@@choices,@@caption)

```

which uses the global **`inputbox()`** retry wrapper method defined in “`Tools/sketchup.rb`”. So then the “`sketchup.rb`” file must be loaded first.

If y’all prefer, you can call the **`UI.inputbox()`** module method directly and bypass the retry wrapper.
