Custom error in a custom function

88 views
Skip to first unread message

Andrew Apell

unread,
Feb 13, 2020, 3:36:48 AM2/13/20
to Google Apps Script Community
Hey guys,
I'm trying to throw a custom error with the code below but nothing is working:

function myFunction(x) {
 
try {
   
if (x == 1) {
     
throw "CustomError";
   
}
   
return x;
 
} catch (e) {
   
return e;
 
}
}



Has anyone successfully tried this before?

Andrew Roberts

unread,
Feb 13, 2020, 4:45:50 AM2/13/20
to google-apps-sc...@googlegroups.com
I don't think you can return from a catch, you can only re-throw the error. 

I'd do something like:

    function myFunction(x) {
      try {
        if (x === 1) {
          var customError = new Error("CustomError")
          customError.isCustom = true
          throw customError
        }
        return x;
      } catch (e) {
        if (e.isCustom) {
          throw new Error('This is a custom error: ' + e.message)
        else {
          throw e
        }
      }
    }    


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/6410e0ff-2ada-47eb-ba9c-7678bce2a8ed%40googlegroups.com.

Andrew Apell

unread,
Feb 13, 2020, 4:55:40 AM2/13/20
to Google Apps Script Community
Thanks Andrew... this seems to be working
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

Andrew Apell

unread,
Feb 13, 2020, 7:26:21 AM2/13/20
to Google Apps Script Community
Weird issue I'm getting... when I try to adapt these changes to my previously existing code, it has no effect.
When I deleted all my code and ran "Test add-on", still nothing happened... everything was functioning as if Apps Script was reading my code from another source.
Has anyone experienced this weirdness before?

Alan Wells

unread,
Feb 13, 2020, 10:08:54 AM2/13/20
to Google Apps Script Community
If you have a mismatched block ending curly brace, then the code can run parts of the code unexpectedly.  It can happen that you can get an  uncaught error that still allows the file to be saved.

Andrew Apell

unread,
Feb 13, 2020, 12:52:56 PM2/13/20
to Google Apps Script Community
Something else is at play.
I copied the entire script to a new project and it worked.
When I returned to my old project, it was still behaving weird: all the functions still work even with an empty project!
I'll just wait for it to self-resolve (hopefully)

Alan Wells

unread,
Feb 13, 2020, 12:57:09 PM2/13/20
to Google Apps Script Community
If the code you ran was a saved version (File - Manage Versions), then it will still run that version, even if you deleted every file.  Or is this weird behavior from the "Head" version.

Andrew Apell

unread,
Feb 13, 2020, 1:20:15 PM2/13/20
to Google Apps Script Community
Head version... but I think I have isolated the issue.
I had installed the store version and was also using the unpublished version on the same sheet 😳
But in all fairness it didn't cause me issues the last time I did this.

Alan Wells

unread,
Feb 13, 2020, 1:25:33 PM2/13/20
to Google Apps Script Community
The different projects should be isolated from each other, even if they are running from the same document.  But, I don't know how anyone would officially know for sure if different installed add-ons, or multiple projects in the same document were interfering with each other.

Andrew Apell

unread,
Feb 13, 2020, 1:27:15 PM2/13/20
to Google Apps Script Community
I think it is just something to be avoided
Reply all
Reply to author
Forward
0 new messages