Skip to main content

Posts

Showing posts with the label Exceptions

Java : Exception in thread "main" java.util.ConcurrentModificationException

When Exception in thread "main" java.util.ConcurrentModificationException occurs. package com.ekiras.demo; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String args[]){ // create a new list List<String> list = new ArrayList<String>(); // add 50 items to the list for(int itr=0;itr<50;itr++) list.add("user-"+(itr+1)); // try to remove item from list while iterating the list for(String str : list){ if(str.equals("user-15")){ list.remove(str); } } } } The above code will give the following error. Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859) at java.util.ArrayList$Itr.next(ArrayList.java:831) at testing.Test.main(Test.java:18) How to avoid ConcurrentModificationException Create the list of type CopyOnWriteArrayList , this will create a new copy of list for ...

Exception in thread java.util.ConcurrentModificationException

Points To Remember ConcurrentModificationException  occurs when the you you try to modify the collection you are using or iterating. It can be prevented if you create a new object of that collection and do operations on that and then assign it back to the original object. java.util.ConcurrentModificationException In the following code we will be iterating on the list and trying to remove the null objects from the list. import java.util.*; public class Demo { public static void main(String args[]) { // create an array of string objs String init[] = { "One", "Two", null, null, "One", "Two", null, "Three", "Four", "Two", null }; // create two lists List<String> list = new ArrayList(Arrays.asList(init)); for (String str: list) { if (str == null) list.remove(str); } System.out.println("List value: " + list); } } The above code will throw the following Exception Exception in thread ...

Spring MVC handle Exceptions in Controller using @ExceptionHandler

Points To Remember You can handle all types of runtime exceptions and custom exceptions using  @ExceptionHandler  annotation.  You just need to define the class to the annotation to handle exceptions of that class. Spring MVC handle Exceptions in Controller Here we have a simple Spring MVC application with the following. HomeController wih three actions  throw1 - throws Custom exception throw2 - throws Custom exception throw3 - throws Runtime exception Home Controller package com.ekiras.controller; import java.text.DateFormat; import java.util.Date; import java.util.Locale; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; im...

How to create Nested Custom Exceptions in Java

Points To Rememeber When you have to create a nested or a group of similar Exceptions in your application then  First create a ParentException class and this class should either extend Exception  class or RuntimeException class.  All the nested Exceptions will extend the ParentException and thus will lie in a hierarchy. Scenario There may be times when you want to have your own hierarchy of Custom Exception so that you can manage your application in a better way. Suppose we have a Quiz application where we want to show questions based on a Topic and display questions Creating Nested Custom Exceptions  So we have created three exception classes here. QuixException InvalidQuestionIdException InvalidAnswerException Here Quiz Exception is the Parent of all the other Exceptions. QuizException can catch all the nested Exceptions so that we do not have to catch each of them separately.   class QuizException extends Exception { public QuizException(String message) {...

How to create a Custom Exception in Java

Points To Remember All the custom Exceptions must implement the Exception class or any of its subclass . There are four types of Constructors that you can use while defining your Custom Exception. Ex 1 : Basic Custom Exception In this case we will just created a Custom Exception where we define a Custom Exception and throw it with a message. This message is visible in the stacktrace. class CustomException extends Exception { public CustomException(String message) { super(message); } } public class Test { public static void main(String args[]) { try { throw new CustomException("Throwing Custom Exception"); } catch (CustomException e) { e.printStackTrace(); } try { throw new CustomException(" ThrowingAnother Custom Exception"); } catch (CustomException e) { e.printStackTrace(); } } } The output of the above example is CustomException: Throwing Custom Exception at Test.main(Test.java:12) CustomException: ThrowingAnother Custom Exceptio...

Custom 404 Error Page in Spring MVC

Points To Remember You need to first add the errors in the web.xml  and redirect them to a url. Then you need to map these url to controller to handle them and take necessary actions.  You can either handle exceptions in your controllers as explained in the link. Custom Error Pages Here in this example we are going to create custom error pages in our spring mvc application. So will first of all configure the error codes in web.xml like the following. web.xml <error-page> <error-code>400</error-code> <location>/400</location> </error-page> <error-page> <error-code>404</error-code> <location>/404</location> </error-page> <error-page> <error-code>500</error-code> <location>/500.jsp</location> </error-page> Now when we have configured the error codes and mapped then with the respective url's, we will now create a Controller that will map these error cod...