

% add the PATH to retrieve cls and sty beamer styles files
\makeatletter
\providecommand*{\input@path}{}
\edef\input@path{{../../support/beamer/}\input@path}% prepend
\makeatother

% \documentclass{pharoslides}
% \documentclass[aspectratio=169]{pharoslides} % format 16/9
\documentclass[withvideo]{pharoslides} % format 16/9 + incrustation video

\graphicspath{{./figures/}{../}{../../support/beamer/imgs/}}

%Information to be included in the title page:
\title{Powerful Exceptions: an Overview}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W5S04}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{Exceptions}

\begin{itemize}
\item Really powerful
\item Can be resumed, restarted, and signaled as new exception
\item Two important classes:
\begin{itemize}
\item \code{Error} and \code{Notification}
\end{itemize}

\item For more complete reference, read Deep into Pharo 
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{What You Will Learn}

\begin{itemize}
\item To raise and trap exceptions
\item Some nice helper methods
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{API Overview}

\begin{itemize}
\item Installing an handler 
\end{itemize}

\begin{listing}{}
  [ doSomething ] on: ExceptionClass do: [ :ex | something ]
\end{listing}

\begin{itemize}
\item Raising an exception
\end{itemize}

\begin{listing}{}
anException signal
\end{listing}

\begin{itemize}
\item \code{defaultAction} is executed when an exception occurs and it is not trapped
\end{itemize}

Convenient messages:

\begin{itemize}
\item \code{ensure:}, \code{ifCurtailed:}
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Catching Example}

\begin{listing}{}
  [ do something ] on: ExceptionClass do: [ :ex | something ]
\end{listing}

Example:

\begin{listing}{}
  | x y |
  x := 7.
  y := 0.
  [ x / y ] 
      on: ZeroDivide
      do: [ :exception | Transcript show: exception description; cr.
           0 ]
   > 0
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Signaling an Exception}

To raise an exception: 

\begin{itemize}
\item create an instance of exception
\item send it messages \code{signal} or \code{signal: aMessage}
\end{itemize}

\begin{listing}{}
  (AuthorNameRequest new initialAnswer: 'Stef') signal
  (Warning new messageText: 'Pay attention') signal
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Signaling an Exception}

Usually classes propose a shortcut

\begin{listing}{}
  OutOfMemory signal.
  Warning signal: 'description of the exception'
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Testing That an Exception Occurs}

SUnit offers \code{should:raise:} and \code{shouldnt:raise:} to check occurrence of exceptions.

\begin{listing}{}
testNameOfMonth

  self assert: (Date nameOfMonth: 1) equals: #January.	

  self 
    shouldnt: [ Date nameOfMonth: 2 ] 
    raise: SubscriptOutOfBounds.
  self 
    should: [ Date nameOfMonth: 13 ] 
    raise: SubscriptOutOfBounds.
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Kinds of Exceptions}

\begin{itemize}
\item \code{Error}: all errors (subscript, message not understood, division by zero
\item \code{Halt}: to stop the execution (and get a debugger)
\item \code{Notification}: non fatal exceptions (deprecation, warning, timedout)
\item \code{UnhandledError}: when an error occurs and that it is \textit{not} trapped
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Exceptions are Real Objects}

When you send an unknown message \code{Point new strangeAndBizarre}

\begin{listing}{}
ProtoObject >> doesNotUnderstand: aMessage

  ^ MessageNotUnderstood new 
       message: aMessage;
       receiver: self;
       signal
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Deprecation}

To support API migration, Pharo uses deprecation
When the deprecation setting is on, a warning is raised when a deprecated method is executed

\begin{listing}{}
MenuItem >> title: aString
  "Add a title line at the top of this menu."
  self deprecated: 'Use method addTitle: instead' on: '29 september' in: #Pharo40. 
  self addTitle: aString
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Deprecation Implementation Use}

Create an instance of \code{Deprecation} and signal it

\begin{listing}{}
deprecated: anExplanationString on: date in: version
  "Warn that the sending method has been deprecated"
  (Deprecation
      method: thisContext sender method
      explanation: anExplanationString
      on: date
      in: version) signal
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Exception Sets}

\begin{listing}{}
  [ do some work ]
      on: ZeroDivide, Warning
      do: [ :ex | what you want ]
\end{listing}

Or

\begin{listing}{}
  | exceptionSets |
  exceptionSets := ExceptionSet with: ZeroDivide with: Warning.
  [do some work]
      on: exceptionSets
      do: [ :ex | what you want ]
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Nice Helper: ensure:}

\begin{itemize}
\item How to ensure that an expression is \textbf{always executed} (even if the program fails before)?
\item \code{{[} doSomething {]} ensure: {[} alwaysExecuteThis {]}}
\end{itemize}

\begin{listing}{}
spyOn: aBlock
  "Profile system activity during execution of aBlock."
  self startProfiling.
  aBlock ensure: [ self stopProfiling ]
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Another nice Helper ifCurtailed:}

\begin{itemize}
\item How to ensure that an expression is \textbf{executed only if the program fails} or returns?
\item \code{{[} doSomething {]} ifCurtailed: {[} onProblem {]}}
\end{itemize}

\begin{listing}{}
wait
  "Schedule this Delay, then wait on its semaphore. The current process will be suspended for the amount of time specified when this Delay was created."

  self schedule.
  [ delaySemaphore wait ] ifCurtailed: [ self unschedule ]
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Exception Lookup}

\begin{itemize}
\item Each process has its own exception environment: an ordered list of active handlers
\item Process starts with an empty list
\item \code{ {[} aaaa {]} on: Error do: {[} bbb {]}} adds \code{Error,bbb} to the beginning of the list
\item When an exception is signaled, the system sends a message to the first handler 
\begin{itemize}
\item If the handler cannot handle the exception, the next one is asked
\item If no handler can handle the exception, then the default action is performed
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Handling Exception}

Just for your information ;)

Within an handler \code{{[} aaa {]} on: anExceptionClass do: {[} anHandler {]}}, we can:

\begin{itemize}
\item \textbf{Return} an alternative result for the protected block (\code{return:})
\item \textbf{Retry} the protected block or a different block (\code{retryUsing:})
\item \textbf{Resume} the protected block at the failure point (\code{resume:})
\item \textbf{Pass} the caught exception to the enclosing handler (\code{pass})
\item \textbf{Resignal} a different exception (\code{resignalAs:})
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Returning From an Exception}

\begin{listing}{}
[ Notification signal. 'Value from protected block' ]
  on: Notification
  do: [ :ex |ex return: 'Value from handler' ]
  
  >  'Value from handler'
\end{listing}

We return a different string on normal or notification 
\end{frame}



\begin{frame}[fragile]

\frametitle{Resuming from Resumable Exception}

\code{Warning}, \code{Notification} and subclasses are resumable

\begin{listing}{}
[ Notification signal. 'Value from protected block' ]
		on: Notification
		do: [ :ex | ex resume: 'Value from handler' ]

> 'Value from protected Block'.
\end{listing}

\begin{itemize}
\item \code{Notification signal} raises an exception
\item exception is handled
\item \code{resume:} restores the context and the value returned normally as if the notification did not occur
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{What You Should Know}

\begin{itemize}
\item Exceptions are powerful in Pharo. 
\item Offer a simple API
\end{itemize}

Raising

\begin{listing}{}
anException signal
\end{listing}

	
Installing: 

\begin{listing}{}
[ doSomething ] on: ExceptionClass do: [ :ex | something ]
\end{listing}

\begin{itemize}
\item Helpers
\begin{itemize}
\item \code{{[} doSomething {]} ensure: {[} alwaysDoThis {]}} 
\item \code{{[} doSomething {]} ifCurtailed: {[} onProblem {]}}
\end{itemize}

\end{itemize}
\end{frame}




% \setlength{\textwidth}{\originaltextwidth}
\setbeamertemplate{background canvas}{\insertBorderForHandout}

\begin{frame}[plain]

\begin{minipage}{\originaltextwidth}
\centering
\large


\vspace{2em}

A course by

\vspace{1em}

\hspace{-20px}\includegraphics[height=32px]{inriaLogo}\hspace{3em}\raisebox{7px}{and}\hspace{3em}\raisebox{-6px}{\includegraphics[height=30px]{logoutop}}

\vspace{2em}

in collaboration with

\vspace{1em}

\includegraphics[height=40px]{unisciellogo}\hskip 1.5em%
\raisebox{5px}{\includegraphics[height=30px]{unitlogo}}\hskip 2em%
\raisebox{8px}{\includegraphics[height=24px]{univLilleLogo}}\hskip 2em%
\raisebox{3px}{\includegraphics[height=29px]{logo-IMT}}\hskip 2em%
\raisebox{1px}{\includegraphics[height=32px]{minesDouaiLogo}}

\vspace{2.5em}

\includegraphics[width=50px]{by-nc-nd} \tiny \hskip 0.5em \raisebox{5px}{Inria 2016}

\medskip

\tiny
Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France

\url{https://creativecommons.org/licenses/by-nc-nd/3.0/fr/} 

\end{minipage}
\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
