ViS
ViS

Reputation: 1517

Custom exception class does not catch exceptions

I have a Custom exception class which extends Exception class (like below).

public class SomeABCException extends Exception

But, when I use this SomeABCException in all places where I previously used Exception in the catch block, it does not catch the Exceptions even after it extends Exception class itself.

E.g. if a parent/caller method has a catch block as below

catch (Exception e) {
    TODO something;
}

and if I have a child method as below in which I am running some database queries.

try {
    Some database queries;
} catch (SomeABCException e) {
    throw new SomeABCException (e.getMessage(),"I/O or SQL_EXCEPTION");
}

Here if the sql connection fails, the catch is not able to catch the exceptions, rather it gets caught in the parent/callers catch block which uses System.Exception to catch it.

During the debug, it does not go to the throw in the catch block on child method.

Please explain, I do not understand it.

Upvotes: 5

Views: 5931

Answers (6)

Mohsin AR
Mohsin AR

Reputation: 3108

Copying @macieji code:

Java exceptions reference super/parent but that has reference of object what actually exception is thrown, you can do following,

class ExceptionA extends Exception {}

class ExceptionB extends Exception {}

class ExceptionC extends ExceptionB {}

try {
  //some code that throws ExceptionA
} catch (Exception ex) {
   if(ex instanceof ExceptionA) {
    // do something for ExceptionA
   } else if(ex instanceof ExceptionB) {
    // do something for ExceptionB
   } else {
    // for Exception
   }
}

Upvotes: 0

Maciej Stępyra
Maciej Stępyra

Reputation: 320

As Hovercraft mentioned - it doesnt work that way. Check this example:

class ExceptionA extends Exception {}

class ExceptionB extends Exception {}

class ExceptionC extends ExceptionB {}

try {
  //some code that throws ExceptionA
} catch (Exception ex) {
   //will work
}


try {
  //some code that throws ExceptionB
} catch (ExceptionA ex) {
  //will not work
}

try {
  //some code that throws ExceptionC
} catch (ExceptionB ex) {
   //that will work
}

So bassically You can catch only exceptions that are throwed in try block or its ancestors.

Upvotes: 2

Sebastian Lapcevic
Sebastian Lapcevic

Reputation: 11

If your "some database queries" method does not throw a "SomeABCException" there's no way to catch an exception that is not thrown.

try {
    Some database queries;
} catch (Exception e) {
    throw new SomeABCException (e.getMessage(),"I/O or SQL_EXCEPTION");
}

"Exception e" should be an specific exception. Or if you want make your queries throw the exception so you can catch your custom exception just like you did.

try {
    Some database queries; // Make sure this method throws your custom exception
} catch (SomeABCException e) {
    //do something else with the exception.
}

Upvotes: 1

Angela
Angela

Reputation: 316

It happens because the method that you are using doesn't throw the SomeABCException

Pay attention that all exceptions extend the Exception class.

Let me explain you with an example:

public void mymethod() throws IOException{
   //...
}

In your code if you try to do:

try{
    myObj.myMethod()
} catch (NumberFormatException ne){
   //... 
}

you will not able to compile. In any case the code inside the catch will never call.

Upvotes: 1

Cacho Santa
Cacho Santa

Reputation: 6914

Try this instead:

try {
    //Some database queries; -> this will throw Exception
} catch (Exception e) {
    throw new SomeABCException (e.getMessage(),"I/O or SQL_EXCEPTION");
}

You are catching SomeABCException when your "some database queries" are not throwing that kind of exception (maybe some SQLException or something like that)

Upvotes: 1

James Wierzba
James Wierzba

Reputation: 17548

I believe you are thinking backwards. Instances of Exception will only catch exceptions that are subclasses, or instances of it. So, since Exception is a superclass of SomeABCException, the SQLException will NOT be caught.

Upvotes: 1

Related Questions