Search notes:

Testing unhandled errors

This test tries to demonstrate how a sub or function needs to be written with an error handler in conjuction with VBA debugging for hierarchical debugging.
option explicit

global dbg_ as new dbg

sub main() ' {
  on error goto err_

    dim fileWriter as new dbgFileWriter
    fileWriter.init(environ("TEMP") & "\error-handling.txt")
    call dbg_.init(fileWriter)
    dbg_.indent("main")

    sub_one
    sub_two
    sub_three

    dbg_.dedent
    exit sub

  err_:
    dbg_.dedent

    msgBox("Error: "& err.description)

end sub ' }

sub sub_one() ' {
  on error goto err_
    dbg_.indent("sub_one")

    dbg_.dedent
    exit sub

  err_:
    dbg_.unhandledError
end sub ' }

sub sub_two() ' {
  on error goto err_
    dbg_.indent("sub_two")

    sub_sub

    dbg_.dedent
    exit sub

  err_:
    dbg_.unhandledError
end sub ' }

sub sub_three() ' {
  on error goto err_
    dbg_.indent("sub_three")

    dbg_.dedent
    exit sub

  err_:
    dbg_.unhandledError
end sub ' }

sub sub_sub() ' {
  on error goto err_
    dbg_.indent("sub_sub")

    sub_sub_sub ( 6)
    sub_sub_sub ( 0) ' boom
    sub_sub_sub (15)

    dbg_.dedent
    exit sub

  err_:
    dbg_.unhandledError
end sub ' }

sub sub_sub_sub(p as long) ' {
  on error goto err_
    dbg_.indent("sub_sub_sub, p = " & p)

    dbg_.text "30 / " & p & " = " & (5/p)

    dbg_.dedent
    exit sub

  err_:
    dbg_.unhandledError
end sub ' }
Github repository VBAModules, path: /dbg/tests/error-handling.bas
2019-01-21 11:05:27 { main
2019-01-21 11:05:27   { sub_one
2019-01-21 11:05:27   }
2019-01-21 11:05:27   { sub_two
2019-01-21 11:05:27     { sub_sub
2019-01-21 11:05:27       { sub_sub_sub, p = 6
2019-01-21 11:05:27         30 / 6 = 0.833333333333333
2019-01-21 11:05:27       }
2019-01-21 11:05:27       { sub_sub_sub, p = 0
2019-01-21 11:05:27         Caught unhandled error: Division by zero
2019-01-21 11:05:27       }
2019-01-21 11:05:27       Caught unhandled error: Division by zero
2019-01-21 11:05:27     }
2019-01-21 11:05:27     Caught unhandled error: Division by zero
2019-01-21 11:05:27   }
2019-01-21 11:05:27 }

See also

dbg tests

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759430764, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/VBA/modules/dbg/tests/unhandled-errors(147): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78