The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

GX::Exception - Exception class

SYNOPSIS

    # Load the module
    use GX::Exception;
    
    # Try
    eval {
        throw "Oops!";
    };
    # Catch
    if ( $@ ) {
        warn "Exception: $@";
    }
    
    # Same as above, but with proper localization of $@
    try {
        throw "Oops!";
    }
    catch {
        warn "Exception: $_";
    };

DESCRIPTION

This module provides the GX::Exception class.

METHODS

Constructor

new

Returns a new GX::Exception object.

    $exception = GX::Exception->new( %attributes );
Attributes:
  • message ( string )

    An error message.

  • stack_trace ( GX::Exception::StackTrace object )

    A stack trace object.

  • subexception ( GX::Exception object | string )

    A subexception.

  • verbosity ( integer )

    The default verbosity level. Possible values are: "0" (only prints the error message), "1" (prints the error message and also the error messages of all subexceptions), "2" (prints a short stack trace) or "3" (prints a full stack trace). The default value is "0".

Returns:

Alternative syntax:

    $exception = GX::Exception->new( $message );
    $exception = GX::Exception->new( $subexception );
Arguments:
  • $message ( string )

    An error message.

  • $subexception ( GX::Exception object )

    A subexception.

Returns:

Public Methods

as_string

Returns a text representation of the exception.

    $string = $exception->as_string( $verbosity );
Arguments:
  • $verbosity ( integer ) [ optional ]

    Possible values: "0", "1", "2" or "3". Defaults to the value returned by verbosity().

Returns:
  • $string ( string )

In list context, as_string() returns individual lines of text.

    @strings = $exception->as_string( ... );
Returns:
  • @strings ( strings )

complain

Raises the exception just like throw(), but with the stack trace originating at the caller.

    $exception->complain;

This method can also be called as a class method. See throw() for details.

message

Returns / sets the error message.

    $message = $exception->message;
    $message = $exception->message( $message );
Arguments:
  • $message ( string ) [ optional ]

Returns:
  • $message ( string )

stack_trace

Returns / sets the associated stack trace object.

    $stack_trace = $exception->stack_trace;
    $stack_trace = $exception->stack_trace( $stack_trace );
Arguments:
Returns:

In list context, stack_trace() returns a list with the individual stack frame objects.

    @frames = $exception->stack_trace( ... );
Returns:

subexception

Returns / sets the associated subexception.

    $subexception = $exception->subexception;
    $subexception = $exception->subexception( $subexception );
Arguments:
  • $subexception ( GX::Exception object | string | undef ) [ optional ]

Returns:

throw

Throws the exception by calling die().

    $exception->throw;

This method can also be called as a class method:

    GX::Exception->throw;
    GX::Exception->throw( $message );
    GX::Exception->throw( $subexception );
    GX::Exception->throw( %attributes );

This will create a new instance of the exception class (see new() for details) on which throw() is then called.

verbosity

Returns / sets the default verbosity level.

    $verbosity = $exception->verbosity;
    $verbosity = $exception->verbosity( $verbosity );
Arguments:
  • $verbosity ( integer ) [ optional ]

    Possible values: "0", "1", "2" or "3".

Returns:
  • $verbosity ( integer )

EXPORTS

Functions

The following functions are exported by default.

catch

This function is meant to be used with try(). It simply returns the first argument passed to it, which must be a block of code.

    try {
        # Block of code ...
    }
    catch {
        # Block of code ...
    };

complain

Throws an exception with the the stack trace originating at the caller.

    complain $message;
    complain $subexception;
Arguments:

throw

Throws an exception.

    throw $message;
    throw $subexception;
Arguments:

try

This function expects a "try" block as its first argument and optionally a "catch" block as its second argument (see catch() above).

    try {
        # Block of code ...
    };

SEE ALSO

AUTHOR

Jörg A. Uzarek <uzarek@runlevelnull.de>

COPYRIGHT AND LICENSE

Copyright (c) 2009-2011 Jörg A. Uzarek.

This module is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License Version 3 as published by the Free Software Foundation.