-5

I am new to Java and on the Exception topics I have some questions:

  1. If Exception class can catch all the exceptions then why do we have so many exceptions
    defined ?
  2. Exception class is in java.lang package and IOException is in java.io package and
    EmptyStackException is in java.util package. Why they are in different packages?
Eypros
  • 5,370
  • 6
  • 42
  • 75
ankit
  • 380
  • 4
  • 16

4 Answers4

1

If Exception class can catch all the exceptions then why do we have so many exceptions defined

So code can catch specific exceptions to do specific things. Like this:

 try{
    ...
 } catch(FileNotFoundException e) {
    logger.log("No such file or permission denied {}",file);
 } catch(IOException e) {
    logger.log("I/O error while reading file {}",file);
 }

Why they are in different packages

Because packages group together conceptually similar classes. As Exception is a broad concept, different exception classes need not be similar to each other. It would be wrong for all exception classes to belong to the same package.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
0

Because in most cases; you'll need to know exactly what went wrong. Was it caused by wrong input, was a service you're trying to reach not available? For each different type of exception, you might want to implement a different handling.

If, for instance, the user provides invalid input, something's wrong, and you may want to erase the wrong information. If the DB is not accessible, you may want to write it away in memory, and re-try later, when the db becomes accessible.

For a technical exception (database not accessible, .... ) you don't want the same handling as for a business exception (user providing input that makes no sense, for instance).

After all, for the first type, you are responsible. you'll need to de-bug your code. For the second type, your user just should read the users manual, and learn to work with the software.

These are just examples, tons of them around.

Jakob S
  • 19,575
  • 3
  • 40
  • 38
Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

The programming model that Java implements allows program design that can both detect and handle exceptions ... not just detect and commit program suicide. Rather than have the program have to do some sort of deep inspection of a single exception to figure out what went wrong, the different exceptions allow code to be written that can easily handle specific conditions.

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
0

Because you may need specialized exceptions.

For isntance, IOException tells you that the encountered exception has something to do with IO. Also, when catching an exception, you might want to hace a different behavior for an IOException and an ExecutionException:

try{ 
    // ... 
} catch (IOException e) {
    // do something for IOException
} catch (ExecutionException e) {
    // do something for ExecutionException
}

In addition, some Exception may have specific attribute: you can create an Exception when reading a file such that it has a int line attribute indicating which line provoked the exception.

In Java, the good practive is to create your class in the package that makes the most sense for the class itself, whether the class implements an interface in an other package or not.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118