# perldoc > Exception::Class

---
type: CommandReference
command: Exception::Class
mode: perldoc
section: 
source: perldoc
---

## Quick Reference

- `use Exception::Class ( 'MyException' )` — declare a simple exception class
- `use Exception::Class ( 'MyException' => { isa => 'ParentException' } )` — declare with inheritance
- `use Exception::Class ( 'MyException' => { fields => [ 'field1', 'field2' ] } )` — declare with custom fields
- `use Exception::Class ( 'MyException' => { alias => 'throw_my' } )` — declare with an alias subroutine
- `MyException->throw( error => 'message' )` — throw an exception
- `eval { ... }; if ( my $e = Exception::Class->caught('MyException') ) { ... }` — catch without Try::Tiny
- `try { ... } catch { if ( $_->isa('MyException') ) { ... } }` — catch with Try::Tiny
- `Exception::Class->Classes` — list all classes created via import

## Name

A module that allows you to declare real exception classes in Perl.

## Synopsis

perl
use Exception::Class (
    'MyException',

    'AnotherException' => { isa => 'MyException' },

    'YetAnotherException' => {
        isa         => 'AnotherException',
        description => 'These exceptions are related to IPC'
    },

    'ExceptionWithFields' => {
        isa    => 'YetAnotherException',
        fields => [ 'grandiosity', 'quixotic' ],
        alias  => 'throw_fields',
    },
);
use Scalar::Util qw( blessed );
use Try::Tiny;

try {
    MyException->throw( error => 'I feel funny.' );
}
catch {
    die $_ unless blessed $_ && $_->can('rethrow');

    if ( $_->isa('Exception::Class') ) {
        warn $_->error, "\n", $_->trace->as_string, "\n";
        warn join ' ', $_->euid, $_->egid, $_->uid, $_->gid, $_->pid, $_->time;
        exit;
    }
    elsif ( $_->isa('ExceptionWithFields') ) {
        if ( $_->quixotic ) {
            handle_quixotic_exception();
        }
        else {
            handle_non_quixotic_exception();
        }
    }
    else {
        $_->rethrow;
    }
};

# without Try::Tiny
eval { ... };
if ( my $e = Exception::Class->caught ) { ... }

# use an alias - without parens subroutine name is checked at compile time
throw_fields error => "No strawberry", grandiosity => "quite a bit";
## Options

### Declaration Options (passed to `use Exception::Class`)

- `isa` — Parent class name. Default: `$Exception::Class::BASE_EXC_CLASS`
- `fields` — Additional attributes for the exception class. Can be a scalar or array reference. Each field must be a legal Perl identifier. Creates an accessor method.
- `alias` — Creates a subroutine in the caller's namespace. Calling it is equivalent to `Class->throw(@_)`. Allows compile-time checking when called without parentheses.
- `description` — Fixed string returned by `description()` method. Describes the exception class, not any particular object.

### Functions

- `Exception::Class->caught( $class )` — Returns the last thrown exception if it is of the given class (or a subclass). Without arguments, returns `$@`. Returns a copy, so `$@` is safe.
- `Exception::Class->Classes` — Returns a list of all classes created via `import` (not subclasses defined manually with `@ISA`).

## Examples

### Declare and throw exceptions

perl
package Foo::Bar::Exceptions;

use Exception::Class (
    'Foo::Bar::Exception::Senses' =>
        { description => 'sense-related exception' },

    'Foo::Bar::Exception::Smell' => {
        isa         => 'Foo::Bar::Exception::Senses',
        fields      => 'odor',
        description => 'stinky!'
    },

    'Foo::Bar::Exception::Taste' => {
        isa         => 'Foo::Bar::Exception::Senses',
        fields      => [ 'taste', 'bitterness' ],
        description => 'like, gag me with a spoon!'
    },
);
### Using `alias` for compile-time checking

perl
use Exception::Class (
    'ExceptionWithFields' => {
        fields => [ 'grandiosity', 'quixotic' ],
        alias  => 'throw_fields',
    },
);

throw_fields error => "No strawberry", grandiosity => "quite a bit";
## See Also

- [Exception::Class::Base](https://metacpan.org/pod/Exception%3A%3AClass%3A%3ABase) — default base class for exception objects
- [Try::Tiny](https://metacpan.org/pod/Try%3A%3ATiny) — recommended try/catch syntax
- [Throwable](https://metacpan.org/pod/Throwable) — modern alternative for Moose/Moo code
- [Scalar::Util](https://metacpan.org/pod/Scalar%3A%3AUtil) — provides `blessed` for checking object type