Skip to main content

Posts

Showing posts with the label exception handling

Exception Handling in RESTful Web Services

Introduction Exception Handling in RESTful webservices is very interesting as well as important topic while writing RESTful web services. Whenever any exception occurs in our web service, by default it will show error with HTTP error code with the description which user can’t understand easily. Why one should handle Exceptions in Restful Web services? User should understand if any error occurred in the system and he/she should easily understand what to do next so that error will not come again as most of the error are due to wrong input given by user and if sometimes error is due to our application he should clearly know that. How to Handle Exceptions in Restful Web services? In RESTful web services, usually response will be in JSON format. So, we need to create response object which will give error in JSON format. Woooooh So let’s get hand dirty. ------------------------------------------------------------------------------------------------- //ErrorRes...

User Defined Exception Handling

Introduction: To develop the project, we must create our own Exception class, as Sun microsystem has not given any exception class some requirements which are not in common. So, what is User defined exception is? Exception class defined by Developer is called as User Defined Exception or Custom exception Steps to create User Defined Exception: Write a class that extends java.lang.Exception. In User defined class, define minimum 2 constructors. No-argument constructor String parameter with logic “super(message)” to create exception object with message.  No need to define any method, why because exception class is not meant for developing business logic , instead it is used for terminating method logic execution by throwing it’s object. ------------------------------------------------------------------------------------------------------------ package com.app.exceptions; /**  *  * @author Shashank Mungantiwar  * ...

Exception Propagation and throw-throws keyword

Exception Propagation: Sending exception from one method to another method is called exception propagation Here propagation is nothing but sending. So guys what is the situation when we are going to implement this propagation concept? Answer to this question is when we throw an exception, if it is not caught in that method it is propagated to another method and interesting thing is that we don’t need to do anything in this process we just need to throw an exception that’s it. Rule: If the propagated exception is checked exception then not only current method developer but also method’s caller method developer should caught or report that checked exception else it leads to same compiler exception….CE: Unreported Exception So according to that basic rule is “If checked exception is raised the method either directly by using throw keyword or indirectly by a method call it must be caught or reported. void m1() throws ClassNotFoundException{ ...