User Tools

Site Tools


dev:error_handling

This is an old revision of the document!


Error Handling

In CP2K there exist three convenient macros for error handling. They are defined in cp2k/src/common/cp_common_uses.f90, and are therefore always available. However, these macros can only be used for short messages, because of Fortran's line length limitation.

   CPWARN("A short warning message")
   CPABORT("A short error message")
   CPASSERT(2>1) ! For sanity checks only, not for user errors.

For longer messages one can use these two routines:

   CALL cp_warn(__SHORT_FILE__,__LINE__,"A long warning message "\\&
                "which can span multiple lines and contains "\\&
                "some valuable advice for the user.")
 
   CALL cp_abort(__SHORT_FILE__,__LINE__,"A long error message "\\&
                 "with even more valuable advises.")      

Cheat Sheet

If you want to…

  • print a short warning message, then you should use:
     CPWARN("your short message")
  • print a longer warning message, then you should use:
     CALL cp_warn(__SHORT_FILE__,__LINE__,"you lengthy message, "//&
                  "which can even span multiple lines")
  • stop the program with a short error message, then you should use:
     CPABORT("your short message")
  • stop the program with a longer error message, then you should use:
     CALL cp_abort(__SHORT_FILE__,__LINE__,"you lengthy message, "//&
                   "which can even span multiple lines")
  • have a simple assertion without a custom error message, then you should use:
     CPASSERT(you_logical_expression)
  • have a simple assertion with a short error message, then you should use:
     IF(.NOT.you_logical_expression) CPABORT("your short message")
  • have a complex assertion with a short error message, then you should use:
     IF(.NOT.your_super_complicated_logical_expression_that_takes_alot_of_space)&
        CPABORT("your short message")
  • have a lengthy assertion with a longer error message, then you should use:
     IF(.NOT.your_super_long_logical_expression_that_takes_alot_of_space)&
        CALL cp_abort(__SHORT_FILE__,__LINE__,"you lengthy message"//&
                      "which again can even span multiple lines")
dev/error_handling.1443449393.txt.gz · Last modified: 2020/08/21 10:14 (external edit)