title: How to set up a callback function when an Exception occurs?
body: <p>I've been stuck for a while on how to set up a callback when an exception occurs. </p>
  
  <p>I have this test code:</p>
  
  <pre><code>void main()
  {
      long * bad = (long*)0x0A000000; //Invalid address
  
      //When the following line gets executed 
      //it causes an error and the debugger sends me to an assembly file. 
      *bad = 123456789; 
  }
  </code></pre>
  
  <p>The assembly file that I am sent to looks like this(fragment of the real file):</p>
  
  <pre><code>.macro DEFAULT_ISR_HANDLER name=
    .thumb_func
    .weak \name
  \name:
  1: b 1b /* endless loop */
  .endm
  
  DEFAULT_ISR_HANDLER SRC_IRQHandler   /*Debugger stops on this line*/
  </code></pre>
  
  <p>As I understand DEFAULT_ISR_HANDLER is a macro that defines an endless loop.
  What I want to do is define my own function in a C file, that I could call when an exception occurs, instead of calling whats defined in the DEFAULT_ISR_HANDLER macro. </p>
  
  <p>My question is, How would I define a macro, in that assembly, that calls an specific C function? </p>
  
  <p>Hopefully I explained myself. Any information or direction around this topic is appreciated.</p>
  
  <p>In case it's relevant I am using GCC ARM compiler v5.4_2016q3</p>
  
  <p>Thanks,
  Isaac</p>
  
  <p>EDIT</p>
  
  <p>I am using a Cortex-M3. </p>
  
  <p>Until now I realized I was talking about processor exceptions. According to the datasheet there is a list with 16 exception types.</p>
  
  <p>Apparently, the way it works is that all the exception types are being redirected to the macro, which in turn calls some thumb function and afterwards an endless loop(according to DEFAULT_ISR_HANDLER above in code). </p>
  
  <p>What I would like to do is define my own function in a C file, for convenience, so every time any type of processor exception appear, I could control how to proceed. </p>