Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I not catch a specific or custom exception?

I dont want to catch some exception. Can I do it somehow?

Can I say something like this:

catch (Exception e BUT not CustomExceptionA)
{
}

?

like image 227
silla Avatar asked Sep 06 '12 13:09

silla


People also ask

Can we catch custom exception in Java?

Java provides us the facility to create our own exceptions which are basically derived classes of Exception. Creating our own Exception is known as a custom exception or user-defined exception. Basically, Java custom exceptions are used to customize the exception according to user needs.

Should you catch specific exceptions?

A good rule of thumb is that you should only catch exceptions that you can properly deal with yourself. If you cannot handle the exception completely then you should let it bubble up to someone who can.

What happens if you don't catch an exception?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console.

Does catch exception catch all exceptions?

yeah. Since Exception is the base class of all exceptions, it will catch any exception.


2 Answers

try
{
      // Explosive code
}
catch (CustomExceptionA){ throw; }
catch (Exception ex)
{
    //classic error handling
}
like image 58
Steve B Avatar answered Oct 07 '22 01:10

Steve B


try
{
}
catch (Exception ex)
{
    if (ex is CustomExceptionA)
    {
        throw;
    }
    else
    {
        // handle
    }
}
like image 45
abatishchev Avatar answered Oct 06 '22 23:10

abatishchev