When do we rethrow exception in catch block with different exception

Hi,

When do we rethrow exception in catch block with different exception?

Say


class ExceptionClass
{
public static void main(String[] args){
try {
System.out.println(9/0);
}
catch(ArithmeticException e) {
throw new NullPointerException();
}
}
}

I wonder whether we can do like above and in what situations we do like above?
please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
If you have a method that declares a data access exception for example in its throws clause.

If the body of the method catches a SQLException, you will probably want to wrap that in data access exception before throwing it out to the caller. You would not want the caller to handle SQLException.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
Any simple example on this?
gurpsbassiCommented:
exactly what I said:

public void doSomethingDangerous() throws DataAccessException{

    try{
            ...something that can cause a SQLException
         }catch(SQLException e){
          throw new DataAccessException(e);
        }

}
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

gudii9Author Commented:
What's dataaccessexceptiob?I never heard? Is it custom exception?
gurpsbassiCommented:
Ok, lets call it Guddi9Exception.
gudii9Author Commented:
If you have a method that declares a data access exception for example in its throws clause.

If the body of the method catches a SQLException, you will probably want to wrap that in data access exception before throwing it out to the caller. You would not want the caller to handle SQLException.

we would want caller to handle data access exception instead right?
gurpsbassiCommented:
Yes. The caller may decide the handle it or let it bubble up the call stack and simply do nothing
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.