SlideShare uma empresa Scribd logo
1 de 13
Exceptions

   Lightning Talk on Exceptions
   MiltonKeynes.pm 12-Jan-2009
   Peter Edwards
    peter@dragonstaff.co.uk




   Slides at http://perl.dragonstaff.co.uk
What Are Exceptions

   Defensive programming
       expected behaviour
       you should anticipate bugs or incorrect input
       recovery from
        or reporting of
        "exceptions" to expected behaviour
Exception Handling

   detect
       check data and die/throw exception when invalid
       wrap calls in eval:
          eval { $foo->bah() };
          die "foo failed: $@" if $@;
   classify
       user error
        program error
       severity level (debug, info, warn, error, fatal)
Exception Handling

   report
       warn to STDERR and continue
       die with message to STDOUT
       print to screen
       logging with log4perl - severity level
Exception Methods

   Return codes
       don't do this!
       is it 0 or 1 or a special value that is failure?
       system calls vary
       easy to forget to check return code in caller leading
        to hidden error
       difficult to return code and message
            sub a { return (1, "Invalid file format, file path: $fpath"); }
            my $rc = a(); # oops, only gets message not code
Exception Methods
   hierarchy of die/rethrow inside eval
       sub a {
           die "oops"; # should be a croak() for context
           return 1;
        }
        sub b {
           my $rc;
           eval { $rc = a() }; # undef
           die "a() failed: $@" if $@; # rethrow
           return $rc;
        }
        eval { print "b returns " . b() };
        print "b() failed: $@" if $@;
        >>> b() failed: a() failed: oops at t.pl line 2.
Better Exceptions

   eval/die/rethrow is a bit clunky
   built-in error object methods would be nice
   languages like java or javascript give you
    try... catch... throw
   so use a CPAN exception module... which one?
Modules available

   Fatal - makes failed builtins throw exceptions
       use Fatal qw( open close );

   Error
       gives you try... catch... throw
       obscure nested closure memory leak
Modules available

   Class::Throwable
       lightweight
       easy to use
       controllable levels of verbosity
       extensible exception objects
       I use this one
Modules Available

   Exception::Class
       full-powered
       complex
       may be a performance overhead
Modules Available

   Others include: Exception

   Good discussion of pros/cons in
    Class::Throwable perldoc
Gotcha

   DIE blocks can reset $@
       sub check {
           croak ”invalid parameter” unless $_[0];
        }
        sub DESTROY {
           local $@; # forget me and you've had it
           eval { somefoo() };
           print ”in DESTROYn”;
        }
        eval { check(undef) };
        print ”$@n”;
        >>> (empty)
   CPAN solution: Devel::EvalError
Perl Best Practices

   ”Perl Best Practices” pp.273-296 first ed. 2005,
    D.Conway pub. O'Reilly
       Ten essential development practices
        #8 Throw exceptions instead of returning special
        values or setting flags
   See also Summary of Chapter 13, Error
    Handling (too many points to list here!)
   -end-

Mais conteúdo relacionado

Mais procurados

Handling error & exception in php
Handling error & exception in phpHandling error & exception in php
Handling error & exception in phpPravasini Sahoo
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php Mudasir Syed
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Mohd Harris Ahmad Jaal
 
Advanced php
Advanced phpAdvanced php
Advanced phpAnne Lee
 
Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)Sharon Levy
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templatesfarhan amjad
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ ConstructorSomenath Mukhopadhyay
 
Introduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable PerlIntroduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable PerlAlex Balhatchet
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)Core Lee
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++imran khan
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 

Mais procurados (20)

Introduction to php exception and error management
Introduction to php  exception and error managementIntroduction to php  exception and error management
Introduction to php exception and error management
 
Handling error & exception in php
Handling error & exception in phpHandling error & exception in php
Handling error & exception in php
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
PerlScripting
PerlScriptingPerlScripting
PerlScripting
 
null Pune meet - Application Security: Code injection
null Pune meet - Application Security: Code injectionnull Pune meet - Application Security: Code injection
null Pune meet - Application Security: Code injection
 
Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
Wtf per lineofcode
Wtf per lineofcodeWtf per lineofcode
Wtf per lineofcode
 
Introduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable PerlIntroduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable Perl
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
Test doubles
Test doublesTest doubles
Test doubles
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 

Destaque

Manual de base de datos
Manual de base de datosManual de base de datos
Manual de base de datosgime96
 
Base de datos gimenez tavella
Base de datos gimenez  tavellaBase de datos gimenez  tavella
Base de datos gimenez tavellagime96
 
Apresentação teleconferência de resultados 2 t10
Apresentação   teleconferência de resultados 2 t10Apresentação   teleconferência de resultados 2 t10
Apresentação teleconferência de resultados 2 t10Braskem_RI
 
Basededatos.pdf
Basededatos.pdfBasededatos.pdf
Basededatos.pdfgime96
 
Base de datos gimenez tavella
Base de datos gimenez  tavellaBase de datos gimenez  tavella
Base de datos gimenez tavellagime96
 
Microchip
MicrochipMicrochip
Microchipgime96
 
ใบงานสำรวจตนเอง M
ใบงานสำรวจตนเอง Mใบงานสำรวจตนเอง M
ใบงานสำรวจตนเอง MFiction Lee'jslism
 

Destaque (9)

Manual de base de datos
Manual de base de datosManual de base de datos
Manual de base de datos
 
Guia 11
Guia 11Guia 11
Guia 11
 
Base de datos gimenez tavella
Base de datos gimenez  tavellaBase de datos gimenez  tavella
Base de datos gimenez tavella
 
Apresentação teleconferência de resultados 2 t10
Apresentação   teleconferência de resultados 2 t10Apresentação   teleconferência de resultados 2 t10
Apresentação teleconferência de resultados 2 t10
 
ขนมจ็อก
ขนมจ็อกขนมจ็อก
ขนมจ็อก
 
Basededatos.pdf
Basededatos.pdfBasededatos.pdf
Basededatos.pdf
 
Base de datos gimenez tavella
Base de datos gimenez  tavellaBase de datos gimenez  tavella
Base de datos gimenez tavella
 
Microchip
MicrochipMicrochip
Microchip
 
ใบงานสำรวจตนเอง M
ใบงานสำรวจตนเอง Mใบงานสำรวจตนเอง M
ใบงานสำรวจตนเอง M
 

Semelhante a Perl exceptions lightning talk

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]ppd1961
 
Red Flags in Programming
Red Flags in ProgrammingRed Flags in Programming
Red Flags in ProgrammingxSawyer
 
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Alex Balhatchet
 
Getting modern with logging via log4perl
Getting modern with logging via log4perlGetting modern with logging via log4perl
Getting modern with logging via log4perlDean Hamstead
 
Exception Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyWen-Tien Chang
 
A exception ekon16
A exception ekon16A exception ekon16
A exception ekon16Max Kleiner
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 TrainingChris Chubb
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingP3 InfoTech Solutions Pvt. Ltd.
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 SubroutinesShaun Griffith
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlHideaki Ohno
 
Code Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured ExceptionsCode Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured ExceptionsJohn Anderson
 
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)Maik Becker
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 

Semelhante a Perl exceptions lightning talk (20)

Getting testy with Perl
Getting testy with PerlGetting testy with Perl
Getting testy with Perl
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Perl_Part5
Perl_Part5Perl_Part5
Perl_Part5
 
Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]
 
Red Flags in Programming
Red Flags in ProgrammingRed Flags in Programming
Red Flags in Programming
 
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
 
Getting modern with logging via log4perl
Getting modern with logging via log4perlGetting modern with logging via log4perl
Getting modern with logging via log4perl
 
Exception Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in Ruby
 
A exception ekon16
A exception ekon16A exception ekon16
A exception ekon16
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 Subroutines
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
 
Code Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured ExceptionsCode Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured Exceptions
 
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 

Mais de Peter Edwards

Enhancing engagement through content
Enhancing engagement through contentEnhancing engagement through content
Enhancing engagement through contentPeter Edwards
 
BBC World Service Twitter OAuth Perl
BBC World Service Twitter OAuth PerlBBC World Service Twitter OAuth Perl
BBC World Service Twitter OAuth PerlPeter Edwards
 
Role based access control
Role based access controlRole based access control
Role based access controlPeter Edwards
 
Getting started with Catalyst and extjs
Getting started with Catalyst and extjsGetting started with Catalyst and extjs
Getting started with Catalyst and extjsPeter Edwards
 
Desperately seeking a lightweight Perl framework
Desperately seeking a lightweight Perl frameworkDesperately seeking a lightweight Perl framework
Desperately seeking a lightweight Perl frameworkPeter Edwards
 
Real world cross-platform testing
Real world cross-platform testingReal world cross-platform testing
Real world cross-platform testingPeter Edwards
 
Open Source for Government - PSEICT Conference - British Council Case Study u...
Open Source for Government - PSEICT Conference - British Council Case Study u...Open Source for Government - PSEICT Conference - British Council Case Study u...
Open Source for Government - PSEICT Conference - British Council Case Study u...Peter Edwards
 

Mais de Peter Edwards (8)

Enhancing engagement through content
Enhancing engagement through contentEnhancing engagement through content
Enhancing engagement through content
 
Twitter oauth
Twitter oauthTwitter oauth
Twitter oauth
 
BBC World Service Twitter OAuth Perl
BBC World Service Twitter OAuth PerlBBC World Service Twitter OAuth Perl
BBC World Service Twitter OAuth Perl
 
Role based access control
Role based access controlRole based access control
Role based access control
 
Getting started with Catalyst and extjs
Getting started with Catalyst and extjsGetting started with Catalyst and extjs
Getting started with Catalyst and extjs
 
Desperately seeking a lightweight Perl framework
Desperately seeking a lightweight Perl frameworkDesperately seeking a lightweight Perl framework
Desperately seeking a lightweight Perl framework
 
Real world cross-platform testing
Real world cross-platform testingReal world cross-platform testing
Real world cross-platform testing
 
Open Source for Government - PSEICT Conference - British Council Case Study u...
Open Source for Government - PSEICT Conference - British Council Case Study u...Open Source for Government - PSEICT Conference - British Council Case Study u...
Open Source for Government - PSEICT Conference - British Council Case Study u...
 

Último

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Último (20)

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 

Perl exceptions lightning talk

  • 1. Exceptions  Lightning Talk on Exceptions  MiltonKeynes.pm 12-Jan-2009  Peter Edwards peter@dragonstaff.co.uk  Slides at http://perl.dragonstaff.co.uk
  • 2. What Are Exceptions  Defensive programming  expected behaviour  you should anticipate bugs or incorrect input  recovery from or reporting of "exceptions" to expected behaviour
  • 3. Exception Handling  detect  check data and die/throw exception when invalid  wrap calls in eval: eval { $foo->bah() }; die "foo failed: $@" if $@;  classify  user error program error  severity level (debug, info, warn, error, fatal)
  • 4. Exception Handling  report  warn to STDERR and continue  die with message to STDOUT  print to screen  logging with log4perl - severity level
  • 5. Exception Methods  Return codes  don't do this!  is it 0 or 1 or a special value that is failure?  system calls vary  easy to forget to check return code in caller leading to hidden error  difficult to return code and message  sub a { return (1, "Invalid file format, file path: $fpath"); }  my $rc = a(); # oops, only gets message not code
  • 6. Exception Methods  hierarchy of die/rethrow inside eval  sub a { die "oops"; # should be a croak() for context return 1; } sub b { my $rc; eval { $rc = a() }; # undef die "a() failed: $@" if $@; # rethrow return $rc; } eval { print "b returns " . b() }; print "b() failed: $@" if $@; >>> b() failed: a() failed: oops at t.pl line 2.
  • 7. Better Exceptions  eval/die/rethrow is a bit clunky  built-in error object methods would be nice  languages like java or javascript give you try... catch... throw  so use a CPAN exception module... which one?
  • 8. Modules available  Fatal - makes failed builtins throw exceptions  use Fatal qw( open close );  Error  gives you try... catch... throw  obscure nested closure memory leak
  • 9. Modules available  Class::Throwable  lightweight  easy to use  controllable levels of verbosity  extensible exception objects  I use this one
  • 10. Modules Available  Exception::Class  full-powered  complex  may be a performance overhead
  • 11. Modules Available  Others include: Exception  Good discussion of pros/cons in Class::Throwable perldoc
  • 12. Gotcha  DIE blocks can reset $@  sub check { croak ”invalid parameter” unless $_[0]; } sub DESTROY { local $@; # forget me and you've had it eval { somefoo() }; print ”in DESTROYn”; } eval { check(undef) }; print ”$@n”; >>> (empty)  CPAN solution: Devel::EvalError
  • 13. Perl Best Practices  ”Perl Best Practices” pp.273-296 first ed. 2005, D.Conway pub. O'Reilly  Ten essential development practices #8 Throw exceptions instead of returning special values or setting flags  See also Summary of Chapter 13, Error Handling (too many points to list here!)  -end-