azizkhani.net

I know that I know nothing

Checked or Unchecked Exceptions?

clock June 3, 2014 22:58 by author Administrator

Some Java books(*) covering exceptions advice you to use checked exceptions for all errors the application can recover from, and unchecked exceptions for the errors the application cannot recover from. In reality most applications will have to recover from pretty much all exceptions including NullPointerException, IllegalArgumentExceptions and many other unchecked exceptions.



The differences between checked and unchecked exceptions are:

  1. Checked exceptions must be explicitly caught or propagated as described in . Unchecked exceptions do not have this requirement. They don't have to be caught or declared thrown. Basic try-catch-finally Exception Handling

  2. Checked exceptions in Java extend the java.lang.Exception class. Unchecked exceptions extend the java.lang.RuntimeException.

 Checked

public class BadUrlException extends Exception {
        public BadUrlException(String s) {
            super(s);
        }
    }


public void storeDataFromUrl(String url){ try { String data = readDataFromUrl(url); } catch (BadUrlException e) { e.printStackTrace(); } } public String readDataFromUrl(String url) throws BadUrlException{ if(isUrlBad(url)){ throw new BadUrlException("Bad URL: " + url); } String data = null; //read lots of data over HTTP and return //it as a String instance. return data; }

Unchecked:
public class BadUrlException extends RuntimeException { public BadUrlException(String s) { super(s); } }

public void storeDataFromUrl(String url){ String data = readDataFromUrl(url); } public String readDataFromUrl(String url) { if(isUrlBad(url)){ throw new BadUrlException("Bad URL: " + url); } String data = null; //read lots of data over HTTP and //return it as a String instance. return data; }


What is the different between Set and List

clock August 16, 2012 21:24 by author Administrator

Set and List explanation

  • Set – اطلاعات به صورت مرتب نشده ولی بدون تکرار نگهداری میشود
  • List – اطلاعات به صورت مرتب شده ولی با اجازه تکراری بودن.

 

 

Set and List Example

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
public class SetAndListExample 
{
    public static void main( String[] args )
    {
    	System.out.println("List example .....");
    	List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("1");
 
        for (String temp : list){
        	System.out.println(temp);
        }
 
        System.out.println("Set example .....");
        Set<String> set = new HashSet<String>();
        set.add("1");
        set.add("2");
        set.add("3");
        set.add("4");
        set.add("1");
        set.add("2");
        set.add("5");
 
        for (String temp : set){
        	System.out.println(temp);
        }        
    }
}

 


Output

List example .....
1
2
3
4
1
Set example .....
3
2
10
5
4

In Set, the stored values are in unordered way, and the duplicated value will just ignored.

 
 


About the author

 Welcome to this web site . This page has two purposes: Sharing information about my professional life such as articles, presentations, etc.
This website is also a place where I would like to share content I enjoy with the rest of the world. Feel free to take a look around, read my blog


Java,J2EE,Spring Framework,JQuery,

Hibernate,NoSql,Cloud,SOA,Rest WebService and Web Stack tech...

RecentPosts

Month List

Sign In