Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7677859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:31:05+00:00 2026-05-31T17:31:05+00:00

I’ve learned from the latest Java developments, that throwing a RuntimeException and handling it

  • 0

I’ve learned from the latest Java developments, that throwing a RuntimeException and handling it the aspect-oriented ways is the current trend in error handling on the service layer. That means, if anything goes wrong, you just throw a RuntimeException or even better, let the Bean Validation do the magic.

Advantage is: You don’t clog up your code with try-catch and if(entity.getName() == nil) checks. Everything is checked in the background which makes your code much more readable.

So I’m wondering, how this would be done in Grails? Sure, if I use .save(failOnError:true), I get a nice ValidationException. But this leads to a very unpleasant default error page which does not improve the usability of the web application at all.

Do I still have to put it in a try-catch block on Controller level? Let’s say the EntityService has a method, which looks like this:

def toggleSomething(String entityId) = {
    if(!someOtherPrerequisite) {
        throw new EntityException("SomeOtherPrerequisite was not satisfied") // extends RuntimeException
    }

    Entity entity = Entity.get(entityId)
    entity.someProperty = somePropertyValue
    entity.save(failOnError:true) // throws a ValidationException
}

Then the controller would do call it like so:

def toggle = {
    try {
        entityService.toggleSomething(params.id)
    }
    catch(e) {
        flashHelper.error 'I'm sorry, something went wrong.'
    }
}

But this seems to be pretty old school when Grails is so new school in so many things. Isn’t there a way to handle RuntimeExceptions a little nicer without clogging up the code with try-catch?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:31:07+00:00Added an answer on May 31, 2026 at 5:31 pm

    The pattern I follow is:

    1. If the errors can be wrapped up inside a Grails Domain, then don’t deal directly with exception handling. If your service method is primarily dealing with your grails domains, then when there is an error, they wind up in the domain.errors collection. In the controller, just check for those errors (hasErrors()). Since under the covers a RuntimeException is thrown anyway, your transaction is rolled back, no harm, no foul.

    2. If you’re dealing with a 3rd party library (possibly an external web service or such) then don’t be scared of exceptions. Just because Grails (Groovy) doesn’t require you to handle them, they are still good to have / use.

    3. Sometimes neither of the above apply and maybe your service method should just return some true or false flag as to the result of some logic. To take your example, for example:

    Service

        def toggleSomething(String entityId) = {
            if(!someOtherPrerequisite) {
                return false
            }
        }
    
            Entity entity = Entity.get(entityId)
            entity.someProperty = somePropertyValue
            entity.save() // throws a ValidationException
            return entity.hasErrors()
        }
    

    Controller

        def toggle = {
            if (!entityService.toggleSomething(params.id)) {
                flashHelper.error 'I'm sorry, something went wrong.'
            }
        }
    

    Unfortunately, there is probably no solid right or wrong way to approach this. So expect different answers than mine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.