This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Features new in Java 7 and the fly likes Java 7 Exception feature Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "Java 7 Exception feature Query" Watch "Java 7 Exception feature Query" New topic
Author

Java 7 Exception feature Query

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1106
    
    1

Hi

We all know about latest changes made in try-catch block in Java 7.

My query is - If we define catch block as shown below



As we know every reference variable in Java must have a Type associated with it.
Now at compile time, what Type is assigned to 'e' variable by Java Compiler ? Is it SQLException ? Is it IOException ? Or something else?

Also at runtime, how that Type is being resolved to SQLException or IOException?


Also does that type implementation changes if we have one Checked Exception and one RuntimeException in catch block? for example -




~ abhay
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
The answer will doubtless be in the Java Language Specification.That will of course print the runtime type of the Exception, viz FileNotFoundException.
I am sure it will make no difference whether you are catching checked or unchecked Exceptions.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1106
    
    1

Thanks for the reply

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
You're welcome
Did you get your answer about the declared type of those Exceptions?
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
From the specs:

The declared type of an exception parameter that denotes its type with a single class type is that class type.
The declared type of an exception parameter that denotes its type as a union with alternatives D1 | D2 | ... | Dn is lub(D1, D2, ..., Dn) (ยง15.12.2.7).
An exception parameter of a multi-catch clause is implicitly declared final if it is not explicitly declared final.

and "lub" sounds like "least upper bound", as one would guess.

Pankaja Shinde
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
Handling More Than One Type of Exception
In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.

Consider the following example, which contains duplicate code in each of the catch blocks:


In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable ex has different types.

The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:



The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|).

Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.

Bytecode generated by compiling a catch block that handles multiple exception types will be smaller (and thus superior) than compiling many catch blocks that handle only one exception type each. A catch block that handles multiple exception types creates no duplication in the bytecode generated by the compiler; the bytecode has no replication of exception handlers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java 7 Exception feature Query