zwwcn

Just another WordPress.com site

RuntimeException vs Exception

Generally RuntimeExceptions are exceptions that can be prevented programmatically. E.g NullPointerException, ArrayIndexOutOfBoundException. If you check for null before calling any method, NullPointerException would never occur. Similarly ArrayIndexOutOfBoundException would never occur if you check the index first. RuntimeException are not checked by the compiler, so it is clean code.

 

We want to catch the exception and record it but don’t want to show user the error page when error happens. so what we could do is to throw a runtime exception when we catch the exception.

public void test throw customizedRuntimeException(){

try{

somecode which generates error

}catch(IOException e){

throw new customizedRuntimeException(“error msg goes here”);

}

}

then we can catch the customizedRuntimeException:

 

try{

test();

}catch(customizedRuntimeException){

log.error(“some more info”);

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: