SlideShare uma empresa Scribd logo
1 de 14
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 1 of 14
Exception Handling in Perl
Presented by Ian Kluft
Silicon Valley Perl
July 7, 2011
Santa Clara, California
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 2 of 14
Exception Handling in Perl
Three parts to this presentation
● What is exception handling?
● Exception handling modules for Perl
● Using exception handling in your Perl code
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 3 of 14
What is Exception Handling?
● Method of error handling
● Exception is an error which can be
caught by a calling function
● Exception unrolls function calls
until one catches it
● Contains structured data about
error, not just numeric code
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 4 of 14
Exception Handling in the News
● Don't forget to catch exceptions in your code
● Highest profile example: Ariane 501 accident
● June 4, 1996 in Kourou, French Guiana
● ESA space rocket exploded 36 seconds into flight
● Accident investigators found software crashed on
test bench 36 seconds into simulated flight (!!!)
● Integer counter overflow exception was not caught
● Software was only tested with Ariane 4 flight profile
● Ariane 5 overflowed a horizontal motion counter
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 5 of 14
Causes of Exceptions
● Exceptions may be caused by fatal error in
code or system call
● If the program signals an exception itself, it is
called “throwing” an exception
● Most Perl code uses simple form of throwing
exceptions
open FILE, “foo” or die “open failed: $!”
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 6 of 14
Mechanism of Catching Exceptions
● Exceptions can be caught in Perl with eval()
● This is just showing you how it works
● Don't re-invent the wheel
● There are modules for this
eval {
... code to do something ...
};
if ($@) {
handle_error($@);
}
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 7 of 14
Exception Handling Modules
CPAN has many exception handling modules
● Exception::Class is recommended, shown here
● Exception::Lite also currently maintained
● Error adds try/catch style to Perl syntax
● Exception::System – catch system call errors
● Exception (not currently maintained)
● Class::Throwable (not currently maintained)
● Some module sets use own exception handling
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 8 of 14
Exception::Class
● Exceptions are classes inheriting from
Exception::Class or your subclasses of it
● You can organize exceptions hierarchically
● Exception::Class::Base offers handler code
● Using classes sets exceptions at compile time
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 9 of 14
Exception::Class declaration
From manual page:
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',
},
);
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 10 of 14
Exception::Class usage
From manual page:
# try
eval { MyException->throw( error => 'I feel funny.' ) };
my $e;
# catch
if ( $e = Exception::Class->caught('MyException') ) {
warn $e->error, "n", $e->trace->as_string, "n";
warn join ' ', $e->euid, $e->egid, $e->uid, $e->gid,
$e->pid, $e->time;
exit;
} elsif ( $e = Exception::Class->caught('ExceptionWithFields') ) {
$e->quixotic ? do_something_wacky() : do_something_sane();
} else {
$e = Exception::Class->caught();
ref $e ? $e->rethrow : die $e;
}
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 11 of 14
Wrapping main in exception handler
● Good practice... no! This is a best practice.
● To catch all exceptions, make main() an
exception handler wrapper
● Adapt manual page code from previous slide
● Catch Exception::Class exceptions
● Then report unknown errors, like die()
● More graceful exit for program
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 12 of 14
Building depth into error reporting
● Exception classes can be configured to collect
stack traces
● Data structures can be passed from error
recognition point to error reporting code
● Exception::Class::DBI integrates w/ DBI code
● Catch unexpected database errors
● Make a sane error report to user
● Mail notification about database errors to DBAs
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 13 of 14
Exceptions with text descriptions
● All errors can and should have text descriptions
● Error reporting and documentation can be
much more organized
● Extract error documentation from Perl code
● Document for engineers what causes that error
● Document for user/operator what to do when
they see that error
● Document for support dept what it means when
customer calls with that error
Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 14 of 14
Perl Exception Handling: go do it!
● Everything is easier when you design it in
● But this can be added to existing code
● First, add exception-handler wrapper(s)
● Then, define exception classes as needed
● Replace error codes with throwing exceptions
● Both can exist during transition
● Goal: get rid of numeric error codes!

Mais conteúdo relacionado

Mais procurados

Best Practices in Exception Handling
Best Practices in Exception HandlingBest Practices in Exception Handling
Best Practices in Exception HandlingLemi Orhan Ergin
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertionsphanleson
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception HandlingAshwin Shiv
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling ConceptsVicter Paul
 
Exception handling in c programming
Exception handling in c programmingException handling in c programming
Exception handling in c programmingRaza Najam
 
Java Exception Handling and Applets
Java Exception Handling and AppletsJava Exception Handling and Applets
Java Exception Handling and AppletsTanmoy Roy
 
Exception handling
Exception handlingException handling
Exception handlingRavi Sharda
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handlingDeepak Sharma
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
Exception handling
Exception handlingException handling
Exception handlingRaja Sekhar
 
Exceptionhandling
ExceptionhandlingExceptionhandling
ExceptionhandlingNuha Noor
 
Python exceptions
Python exceptionsPython exceptions
Python exceptionsrikbyte
 

Mais procurados (20)

Best Practices in Exception Handling
Best Practices in Exception HandlingBest Practices in Exception Handling
Best Practices in Exception Handling
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertions
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Exception handling in c programming
Exception handling in c programmingException handling in c programming
Exception handling in c programming
 
12 exception handling
12 exception handling12 exception handling
12 exception handling
 
Java Exception Handling and Applets
Java Exception Handling and AppletsJava Exception Handling and Applets
Java Exception Handling and Applets
 
javaexceptions
javaexceptionsjavaexceptions
javaexceptions
 
Exception handling
Exception handlingException handling
Exception handling
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
Exception handling
Exception handling Exception handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Parallel Lint
Parallel LintParallel Lint
Parallel Lint
 
Exception handling
Exception handlingException handling
Exception handling
 
Python exceptions
Python exceptionsPython exceptions
Python exceptions
 

Destaque

08. session 08 intoduction to javascript
08. session 08   intoduction to javascript08. session 08   intoduction to javascript
08. session 08 intoduction to javascriptPhúc Đỗ
 
Geographic Computation in Perl
Geographic Computation in PerlGeographic Computation in Perl
Geographic Computation in PerlIan Kluft
 
Command Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongCommand Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongIan Kluft
 
Stratofox Aerospace Tracking Team presentation at Space Access 2013
Stratofox Aerospace Tracking Team presentation at Space Access 2013Stratofox Aerospace Tracking Team presentation at Space Access 2013
Stratofox Aerospace Tracking Team presentation at Space Access 2013Ian Kluft
 
Geographic Computation in Perl
Geographic Computation in PerlGeographic Computation in Perl
Geographic Computation in PerlIan Kluft
 
Aerospace applications of Perl
Aerospace applications of PerlAerospace applications of Perl
Aerospace applications of PerlIan Kluft
 
Black Rock Desert Impact Theory
Black Rock Desert Impact TheoryBlack Rock Desert Impact Theory
Black Rock Desert Impact TheoryIan Kluft
 

Destaque (7)

08. session 08 intoduction to javascript
08. session 08   intoduction to javascript08. session 08   intoduction to javascript
08. session 08 intoduction to javascript
 
Geographic Computation in Perl
Geographic Computation in PerlGeographic Computation in Perl
Geographic Computation in Perl
 
Command Line Arguments with Getopt::Long
Command Line Arguments with Getopt::LongCommand Line Arguments with Getopt::Long
Command Line Arguments with Getopt::Long
 
Stratofox Aerospace Tracking Team presentation at Space Access 2013
Stratofox Aerospace Tracking Team presentation at Space Access 2013Stratofox Aerospace Tracking Team presentation at Space Access 2013
Stratofox Aerospace Tracking Team presentation at Space Access 2013
 
Geographic Computation in Perl
Geographic Computation in PerlGeographic Computation in Perl
Geographic Computation in Perl
 
Aerospace applications of Perl
Aerospace applications of PerlAerospace applications of Perl
Aerospace applications of Perl
 
Black Rock Desert Impact Theory
Black Rock Desert Impact TheoryBlack Rock Desert Impact Theory
Black Rock Desert Impact Theory
 

Semelhante a Exception Handling in Perl

Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allHayomeTakele
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaKavitha713564
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
Perl exceptions lightning talk
Perl exceptions lightning talkPerl exceptions lightning talk
Perl exceptions lightning talkPeter Edwards
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in JavaVadym Lotar
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingAboMohammad10
 
Java Exception Handling and examples about it
Java Exception Handling and examples about itJava Exception Handling and examples about it
Java Exception Handling and examples about it2022002857mbit
 
Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Netrishisingh190
 
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
 

Semelhante a Exception Handling in Perl (20)

Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
CS3391 -OOP -UNIT – III  NOTES FINAL.pdfCS3391 -OOP -UNIT – III  NOTES FINAL.pdf
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Making Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF UsableMaking Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF Usable
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Perl exceptions lightning talk
Perl exceptions lightning talkPerl exceptions lightning talk
Perl exceptions lightning talk
 
JAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdfJAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdf
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
Core java
Core javaCore java
Core java
 
Java Exception Handling and examples about it
Java Exception Handling and examples about itJava Exception Handling and examples about it
Java Exception Handling and examples about it
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Net
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
PHP - Introduction to PHP Error Handling
PHP -  Introduction to PHP Error HandlingPHP -  Introduction to PHP Error Handling
PHP - Introduction to PHP Error Handling
 
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]
 

Mais de Ian Kluft

"#AprilFools Hijinks" at SVPerl April 2021 meeting
"#AprilFools Hijinks" at SVPerl April 2021 meeting"#AprilFools Hijinks" at SVPerl April 2021 meeting
"#AprilFools Hijinks" at SVPerl April 2021 meetingIan Kluft
 
Secure Coding in Perl
Secure Coding in PerlSecure Coding in Perl
Secure Coding in PerlIan Kluft
 
New Perl module Container::Buildah - SVPerl presentation
New Perl module Container::Buildah - SVPerl presentationNew Perl module Container::Buildah - SVPerl presentation
New Perl module Container::Buildah - SVPerl presentationIan Kluft
 
Securing a Raspberry Pi and other DIY IoT devices
Securing a Raspberry Pi and other DIY IoT devicesSecuring a Raspberry Pi and other DIY IoT devices
Securing a Raspberry Pi and other DIY IoT devicesIan Kluft
 
Best Practices for Recovering Rocket & Balloon Payloads
Best Practices for Recovering Rocket & Balloon PayloadsBest Practices for Recovering Rocket & Balloon Payloads
Best Practices for Recovering Rocket & Balloon PayloadsIan Kluft
 
PiFlash: Linux utility to flash SD cards for Raspberry Pi computers
PiFlash: Linux utility to flash SD cards for Raspberry Pi computersPiFlash: Linux utility to flash SD cards for Raspberry Pi computers
PiFlash: Linux utility to flash SD cards for Raspberry Pi computersIan Kluft
 
Code Generation in Perl
Code Generation in PerlCode Generation in Perl
Code Generation in PerlIan Kluft
 
Pacificon 200905
Pacificon 200905Pacificon 200905
Pacificon 200905Ian Kluft
 

Mais de Ian Kluft (8)

"#AprilFools Hijinks" at SVPerl April 2021 meeting
"#AprilFools Hijinks" at SVPerl April 2021 meeting"#AprilFools Hijinks" at SVPerl April 2021 meeting
"#AprilFools Hijinks" at SVPerl April 2021 meeting
 
Secure Coding in Perl
Secure Coding in PerlSecure Coding in Perl
Secure Coding in Perl
 
New Perl module Container::Buildah - SVPerl presentation
New Perl module Container::Buildah - SVPerl presentationNew Perl module Container::Buildah - SVPerl presentation
New Perl module Container::Buildah - SVPerl presentation
 
Securing a Raspberry Pi and other DIY IoT devices
Securing a Raspberry Pi and other DIY IoT devicesSecuring a Raspberry Pi and other DIY IoT devices
Securing a Raspberry Pi and other DIY IoT devices
 
Best Practices for Recovering Rocket & Balloon Payloads
Best Practices for Recovering Rocket & Balloon PayloadsBest Practices for Recovering Rocket & Balloon Payloads
Best Practices for Recovering Rocket & Balloon Payloads
 
PiFlash: Linux utility to flash SD cards for Raspberry Pi computers
PiFlash: Linux utility to flash SD cards for Raspberry Pi computersPiFlash: Linux utility to flash SD cards for Raspberry Pi computers
PiFlash: Linux utility to flash SD cards for Raspberry Pi computers
 
Code Generation in Perl
Code Generation in PerlCode Generation in Perl
Code Generation in Perl
 
Pacificon 200905
Pacificon 200905Pacificon 200905
Pacificon 200905
 

Último

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 

Último (20)

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

Exception Handling in Perl

  • 1. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 1 of 14 Exception Handling in Perl Presented by Ian Kluft Silicon Valley Perl July 7, 2011 Santa Clara, California
  • 2. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 2 of 14 Exception Handling in Perl Three parts to this presentation ● What is exception handling? ● Exception handling modules for Perl ● Using exception handling in your Perl code
  • 3. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 3 of 14 What is Exception Handling? ● Method of error handling ● Exception is an error which can be caught by a calling function ● Exception unrolls function calls until one catches it ● Contains structured data about error, not just numeric code
  • 4. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 4 of 14 Exception Handling in the News ● Don't forget to catch exceptions in your code ● Highest profile example: Ariane 501 accident ● June 4, 1996 in Kourou, French Guiana ● ESA space rocket exploded 36 seconds into flight ● Accident investigators found software crashed on test bench 36 seconds into simulated flight (!!!) ● Integer counter overflow exception was not caught ● Software was only tested with Ariane 4 flight profile ● Ariane 5 overflowed a horizontal motion counter
  • 5. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 5 of 14 Causes of Exceptions ● Exceptions may be caused by fatal error in code or system call ● If the program signals an exception itself, it is called “throwing” an exception ● Most Perl code uses simple form of throwing exceptions open FILE, “foo” or die “open failed: $!”
  • 6. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 6 of 14 Mechanism of Catching Exceptions ● Exceptions can be caught in Perl with eval() ● This is just showing you how it works ● Don't re-invent the wheel ● There are modules for this eval { ... code to do something ... }; if ($@) { handle_error($@); }
  • 7. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 7 of 14 Exception Handling Modules CPAN has many exception handling modules ● Exception::Class is recommended, shown here ● Exception::Lite also currently maintained ● Error adds try/catch style to Perl syntax ● Exception::System – catch system call errors ● Exception (not currently maintained) ● Class::Throwable (not currently maintained) ● Some module sets use own exception handling
  • 8. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 8 of 14 Exception::Class ● Exceptions are classes inheriting from Exception::Class or your subclasses of it ● You can organize exceptions hierarchically ● Exception::Class::Base offers handler code ● Using classes sets exceptions at compile time
  • 9. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 9 of 14 Exception::Class declaration From manual page: 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', }, );
  • 10. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 10 of 14 Exception::Class usage From manual page: # try eval { MyException->throw( error => 'I feel funny.' ) }; my $e; # catch if ( $e = Exception::Class->caught('MyException') ) { warn $e->error, "n", $e->trace->as_string, "n"; warn join ' ', $e->euid, $e->egid, $e->uid, $e->gid, $e->pid, $e->time; exit; } elsif ( $e = Exception::Class->caught('ExceptionWithFields') ) { $e->quixotic ? do_something_wacky() : do_something_sane(); } else { $e = Exception::Class->caught(); ref $e ? $e->rethrow : die $e; }
  • 11. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 11 of 14 Wrapping main in exception handler ● Good practice... no! This is a best practice. ● To catch all exceptions, make main() an exception handler wrapper ● Adapt manual page code from previous slide ● Catch Exception::Class exceptions ● Then report unknown errors, like die() ● More graceful exit for program
  • 12. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 12 of 14 Building depth into error reporting ● Exception classes can be configured to collect stack traces ● Data structures can be passed from error recognition point to error reporting code ● Exception::Class::DBI integrates w/ DBI code ● Catch unexpected database errors ● Make a sane error report to user ● Mail notification about database errors to DBAs
  • 13. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 13 of 14 Exceptions with text descriptions ● All errors can and should have text descriptions ● Error reporting and documentation can be much more organized ● Extract error documentation from Perl code ● Document for engineers what causes that error ● Document for user/operator what to do when they see that error ● Document for support dept what it means when customer calls with that error
  • 14. Presented at SVPerl July 7, 2011 Exception Handling in Perl By Ian Kluft Slide 14 of 14 Perl Exception Handling: go do it! ● Everything is easier when you design it in ● But this can be added to existing code ● First, add exception-handler wrapper(s) ● Then, define exception classes as needed ● Replace error codes with throwing exceptions ● Both can exist during transition ● Goal: get rid of numeric error codes!