How To Catch 404 Error In Java
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies 404 exception java of this site About Us Learn more about Stack Overflow the company Business
Spring Not Found Exception
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges
Web.xml Error-page
Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:
Notfoundexception
Sign up How to detect 404 response code from IO exception? up vote 2 down vote favorite How are you suppose to detect a 404 from an IO exception. I could just search the error message for "404", but is that the correct way? Anything more direct? import com.google.api.services.drive.model.File; import com.google.api.services.drive.Drive.Files.Update; import com.google.api.services.drive.Drive; File result = null; try { update = drive.files().update(driveId, file httpresponseexception , mediaContent); update.setNewRevision(true); result = update.execute(); } catch (IOException e) { Log.e(TAG, "file update exception, statusCode: " + update.getLastStatusCode()); Log.e(TAG, "file update exception, e: " + e.getMessage()); } Log.e(TAG, "file update exception, statuscode " + update.getLastStatusCode()); 03-03 05:04:31.738: E/System.out(31733): file update exception, statusCode: -1 03-03 05:04:31.738: E/System.out(31733): file update exception, e: 404 Not Found 03-03 05:04:31.738: E/System.out(31733): "message": "File not found: FileIdRemoved", Answer: Aegan's comment below was correct, turns out you can subclass the the exception to a GoogleJsonResponseException and from there get the status code. The answer in this case ultimately depended on the fact I am using a GoogleClient, which generates a subclass of IO Exception that contains the status code. Example: Try{ ... }catch (IOException e) { if(e instanceof GoogleJsonResponseException){ int statusCode = ((GoogleJsonResponseException) e).getStatusCode(); //do something } } java android share|improve this question edited Mar 3 '14 at 13:58 asked Mar 3 '14 at 12:49 NameSpace 4,74811127 Maybe you are getting a subClass of IOException for 404? –DevrimTuncer Mar 3 '14 at 13:36 Correct! I get a GoogleJsonResponseExpection, which when casted to, contains a getStatusCode() method. If
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow java not found exception the company Business Learn more about hiring developers or posting ads with us Stack @exceptionhandler Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of http status codes 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Handle error 404 with Spring controller up vote 8 down vote favorite 4 I use @ExceptionHandler to handle http://stackoverflow.com/questions/22147277/how-to-detect-404-response-code-from-io-exception exceptions thrown by my web app, in my case my app returns JSON response with HTTP status for error responses to the client. However, I am trying to figure out how to handle error 404 to return a similar JSON response like with the one handled by @ExceptionHandler Update: I mean, when a URL that does not exist is accessed java spring spring-mvc share|improve this question edited Nov 13 '12 at 8:09 http://stackoverflow.com/questions/13356549/handle-error-404-with-spring-controller asked Nov 13 '12 at 6:45 xybrek 8,65525142277 add a comment| 4 Answers 4 active oldest votes up vote 3 down vote accepted Simplest way to find out is use the following: @ExceptionHandler(Throwable.class) public String handleAnyException(Throwable ex, HttpServletRequest request) { return ClassUtils.getShortName(ex.getClass()); } If the URL is within the scope of DispatcherServlet then any 404 caused by mistyping or anything else will be caught by this method but if the URL typed is beyond the URL mapping of the DispatcherServlet then you have to either use:
Java testing Databases Java development in cloud Java DevOps Modular code EJB specification Modular containers Modular troubleshooting Programming code App servers Modular tools Java in the cloud Java Web services WS* specs Architecture Java middleware Spring Hibernate http://www.theserverside.com/news/thread.tss?thread_id=29287 Servlets JSP Java client platforms Framework JSF Web portals UI Enterprise Java development JSRs and APIs Languages JVM languages DSLs Java app deployment Java tools Java performance Java performance Cloud platforms JVM tuning Java HPC http://www.avajava.com/tutorials/lessons/how-do-i-create-a-jsp-error-page-to-handle-particular-error-codes.html Web dev tools Development tools Open source strategy IDEs Java platforms Java testing Development hardware Mobile app dev Mobile development and deployment Mobile cloud apps Mobile platforms Mobile ALM Mobile app testing SEARCH this not found site and the web Site Index Home>Web tier: servlets, JSP, Web frameworks>Best way to handle 404 - page not found error in a jsp. Email This Home Web tier: servlets, JSP, Web frameworks: Best way to handle 404 - page not found error in a jsp. Get thread feed Best way to handle 404 - page not found error in a jsp. (15 messages) Posted by: Amit Sharma Posted not found exception on: October 07 2004 08:13 EDT hiThe problem that i am facing is I have a simple test application if a page is not found than it show 404 error but instead of this page i want to show my own custom error page. Reply to this Threaded Messages (15) Use a
handle particular error codes? Tutorial Categories: Ajax (1)Ant (16)Apache Web Server (8)Bioinformatics (10)Cascading Style Sheets (47)Classes and Objects (14)Database (13)Design Patterns (22)Eclipse (39)Files (62)General Java (69)JSPs (9)Java Basics (11)Linux (23)Logging (5)Maven (88)Search (12)Servlets (20)Struts (1)Text (19)Tomcat (8)Version Control (8)Windows (2)XML (1) How do I create a JSP error page to handle particular error codes? Author: Deron Eriksson Description: This Java tutorial describes how to create a JSP to handle particular error codes. Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0) || Tomcat 5.5.20 Page: 1 2> When a particular error code occurs in the context of your JavaSW web application, such as a 404 (Not Found) or 500 (Internal Server Error), it is possible to send the user to an error page. The error code and the page location where the user should be sent in the event of that error can be specified in an error-page section in web.xmlW. Below, we see that when a 404 error occurs, the user is to be redirected to the error-404.jsp. Section of web.xml