Best approach of handling exceptions in Struts2? - struts2

Can anybody tell me the best approach of handling exceptions in Struts2? I've configured global exception mappings in struts.xml for specific exceptions. I couldnt find the best approach of handling exceptions.I have two approaches in mind.
1) putting try/catch in each method and throwing exceptions from catch block 2)Without catching any exceptions ie., declaring exceptions in throws clause of method so that Framework automatically handles exceptions and picks suitable mapping from struts.xml and displays the respective error page.
If anybody knows or has tutorial link on this exception handling, pls provide me.

Catching and subsequently rethrowing an exception is almost never a good idea. Typically, you only want to have try/catch blocks if there is actually something you can do with the caught exception, such as handle it and move on correctly, or adding additional information to the exception.
Adding throws to a struts method is perfectly acceptable, in the event that what it throws is not something you can handle in your code and you just need to display an error page. Unless there is some gain to the overhead of the try/catch, it is better just to let it unwind on out on it's own.

Related

Is try-catch block really needed in MVC application, when we have Exception Filters in MVC?

We have try catch block for error handling, But in case of MVC we have exception filters. In such case we can handle errors at filter level. i.e. by using exception filters.
So again writing try-catch block at method level is really needed?
MVC has exception filter in order to display error pages, rather than allowing the exception to bubble up to IIS (i.e. crashing the whole app), where you'd get the simple (and ugly) generic error pages.
However, the idea is that uncaught exceptions should actually be a rarity. For the most part, you don't actually want to return a 500 error page, even if it's a "pretty" error page. Catching the exceptions in your code allows you to do defensive coding. In other words, you can try to handle and recover from those exceptions, such that your site still works, and at the worst, you give specific and guided messages to the user regarding how to avoid the error.
Long and short, yes, still important to use try-catch blocks, and I'd argue even crucial. Unless, you specifically mean to return a 500 error (there are some occasions, particular in APIs, where that might be desirable), you should catch and handle every exception yourself.

Handling grails exceptions in controllers to catch and forward

I'm developing a grails plugin that among other things it handles the json render of all responses for our REST APIs. We are handling some common response status as exceptions, such as bad requests, forbidden, etc.
By throwing specific exceptions I then process them in an error controller and set the proper response status.
The preblem We are having is that since We are monitoring our APIs through NewRelic, all exceptions are reported as 500 errors.
I saw that in grails 2.3 you can define exception handlers on the cotrollers, and it works, but defining it one by one is out of the questions, and adding the handler through metaClass is not working.
Any ideas to solve this?
I am not sure what you mean by "defining it one by one is out of the questions" but if you mean that you don't want to add the exception handler methods in all of your controllers you have at least a couple of options. One is that you could define the exception handler methods in a base controller which all of your other controllers extend. Another option is to write an AST transformation which adds the methods to all of your controllers at compile time.
I hope that helps.

Exception handling in groovy server pages

I have been searching the web for the issue I'm facing for quite some time now. I found people facing it, but couldn't get an appropriate solution to the problem. The issue is what would be the best way to handle any exception occurred in an template.gsp ? I know I can use a try-catch there so that my controller don't get the exception, but I did find people saying its not a good practice, but failed to answer why. So is it a wrong way and if it is then is there any better solution to deal with this problem ?
The correct way is with a 500 error handler: http://grails.org/doc/latest/guide/theWebLayer.html#mappingToResponseCodes
If you have a lot of logic in your views that could produce a error consider refactoring the code into a tag library which can incorporate better error handling
It depends on what behavior you want for your application with the error.
If a generic error page works for the application - then setup an error page in URL mappings and ignore the error in the controller.
Do you need a nice custom error that is specific for that case - (or possibly display an alternate page? ), if so then you'd need to a try catch (or do some fun with URL mapping and creating an error controller). The objections that come up in Exception handling in Grails controllers arise around the amount of code required for the error handling in the controller vs the rest of the application - with the error handling code being ~40% of each controller method. This causes the code to seem bad/bloated (and apparently violate the CodeNarc "CatchException" rule).

handle exception thrown from constructors in JSF using Handlers

Hello folks,
Though this is my first question, Iam used to reading solutions in stackoverflow and am really pleased to get appropriate answers.
My question is : Is there any way to handle exception thrown from a constructor using some handlers (Instead of writing try and catch in every constructor) and redirect page to an error.xhtml. Iam using JSF2. And how i can identify the exception is thrown from constructor.
Waiting for your reply.
First of all, try to avoid doing anything in the constructor of a managed bean, it's unnecessary and ugly. Annotate a public, no-argument method with the #PostConstruct annotation and that method is guaranteed to run immediately after the managed bean has been instantiated.
To address your main issue, JSF provides an ExceptionHandler class that you can override for the benefit of custom exception handling. It's implemented in a semi-declarative way and applies globally, i.e. for the entire JSF application context. This tutorial provides a good guide on implementing the handler. From that tutorial, note that the ExceptionQueuedEvent object provides all the information available concerning the exception thrown including the JSF Phase in which the exception was thrown and even the component that generated the exception

How to catch an error that isn't even caught by the OnException override?

So we have a base controller we are using and it contains an override for OnException that logs the error to our bug tracking tool. However I noticed that some errors never make it, and I'm wondering if it is somehow silly to expect an application to log all it's own errors when itself is the thing that is having an error, which could cause some errors never to be caught.. ? Either way what is the best solution, or should OnException always be expected to happen? Should I be putting some tool on Windows Server to log the errors instead of within the app itself?
I don't think I've ever seen an error that wasn't trappable at some point. I have application level logging also and like you i've implemented a base controller.
my base controller doesn't do any logging. instead i created a custom action filter with which i decorate my controllers and actions with and it does all the logging.
i also, in the global.asax, added code to the Application_Error event that does a bit of logging and also calls an error controller to handle the actual error.
I hope this in some way helps you. If you want any of the code i've mentioned then add a comment to this answer and i shall provide. it's not a lot of code either.
The Controller.OnException event can only catch events that are thrown in the scope of an executing Controller. There are events that happen before an http request is mapped to a controller and the OnException event will not capture these events. For example, if you have a controller factory that maps the RequestContext to a particular controller, than an exception thrown here is not in the scope of a Controller.
You should implement a "fail safe" exception handler in the global.asax.Application_Error event.

Resources