In addition to the new features mentioned above, try catch in Java 7 is a simple example in this article. In fact, there are still many points to note. First, let's look at a typical code:
Two exception classes are introduced:
Java code
- Public class extends tiona extends exception {
- Public regiontiona (string message ){
- Super (Message );
- }
- }
- Public class exceptionb extends exception {
- Public exceptionb (string message ){
- Super (Message );
- }
- }
public class ExceptionA extends Exception{ public ExceptionA(String message){ super(message); }}public class ExceptionB extends Exception{ public ExceptionB(String message){ super(message); }}
Create another resource class oldresource, as shown below: Java code
- Public class oldresource {
- Public void dosomework (string work) throws javastiona {
- System. Out. println ("doing:" + work );
- Throw new effectiona ("exception occured while doing work ");
- }
- Public void close () throws exceptionb {
- System. Out. println ("Closing the resource ");
- Throw new exceptionb ("exception occured while closing ");
- }
- }
public class OldResource{ public void doSomeWork(String work) throws ExceptionA{ System.out.println("Doing: "+work); throw new ExceptionA("Exception occured while doing work"); } public void close() throws ExceptionB{ System.out.println("Closing the resource"); throw new ExceptionB("Exception occured while closing"); }}
Let's start using: Java code
- Public class oldtry {
- Public static void main (string [] ARGs ){
- Oldresource res = NULL;
- Try {
- Res = new oldresource ();
- Res. dosomework ("writing an article ");
- } Catch (exception e ){
- System. Out. println ("exception message:" +
- E. getmessage () + "exception type:" + E. getclass (). getname ());
- } Finally {
- Try {
- Res. Close ();
- } Catch (exception e ){
- System. Out. println ("exception message:" +
- E. getmessage () + "exception type:" + E. getclass (). getname ());
- }
- }
- }
- }
public class OldTry { public static void main(String[] args) { OldResource res = null; try { res = new OldResource(); res.doSomeWork("Writing an article"); } catch (Exception e) { System.out.println("Exception Message: "+ e.getMessage()+" Exception Type: "+e.getClass().getName()); } finally{ try { res.close(); } catch (Exception e) { System.out.println("Exception Message: "+ e.getMessage()+" Exception Type: "+e.getClass().getName()); } } }}
View output:
Doing: writing an article
Exception message: exception occured while doing work exception type: javaapplication4.exceptiona
Closing the resource
Exception message: exception occured while closing exception type: javaapplication4.exceptionb
Let's take a look at the new writing method in Java 7. The Code is as follows: Java code
- Public class newresource implements autocloseable {
- String closingmessage;
- Public newresource (string closingmessage ){
- This. closingmessage = closingmessage;
- }
- Public void dosomework (string work) throws javastiona {
- System. Out. println (work );
- Throw new partition tiona ("exception thrown while doing some work ");
- }
- Public void close () throws exceptionb {
- System. Out. println (closingmessage );
- Throw new exceptionb ("exception thrown while closing ");
- }
- Public void dosomework (newresource res) throws writable tiona {
- Res. dosomework ("Wow res getting res to do work ");
- }
- }
public class NewResource implements AutoCloseable{ String closingMessage; public NewResource(String closingMessage) { this.closingMessage = closingMessage; } public void doSomeWork(String work) throws ExceptionA{ System.out.println(work); throw new ExceptionA("Exception thrown while doing some work"); } public void close() throws ExceptionB{ System.out.println(closingMessage); throw new ExceptionB("Exception thrown while closing"); } public void doSomeWork(NewResource res) throws ExceptionA{ res.doSomeWork("Wow res getting res to do work"); }}
In Java 7, new features must be automatically used in try catch. If you want to disable resources without writing so many things, you can compile and implement the autocloseable class, and you can use this feature.
Call in the main program:
Java code
- Public class trywithres {
- Public static void main (string [] ARGs ){
- Try (newresource res = new newresource ("RES1 closing ")){
- Res. dosomework ("Listening to podcast ");
- } Catch (exception e ){
- System. Out. println ("exception:" +
- E. getmessage () + "thrown by:" + E. getclass (). getsimplename ());
- }
- }
- }
public class TryWithRes { public static void main(String[] args) { try(NewResource res = new NewResource("Res1 closing")){ res.doSomeWork("Listening to podcast"); } catch(Exception e){ System.out.println("Exception: "+ e.getMessage()+" Thrown by: "+e.getClass().getSimpleName()); } }}
Output result:
Listening to podcast
RES1 closing
Exception: exception thrown while doing some work thrown by: interval tiona
You can think about the reason for this output. In the new feature, close () is called automatically when the resource is closed.
Newresource res = new newresource ("RES1 closing ")){
The value of closingmessage has been assigned, and the final exception e is output, and suprred is dropped.
Output of exception A and exception B.
Let's look at a multi-layer nested try catch example.
Java code
- Public class trywithres {
- Public static void main (string [] ARGs ){
- Try (newresource res = new newresource ("RES1 closing ");
- Newresource RES2 = new newresource ("RES2 closing ")){
- Try (newresource nestedres = new newresource ("nestedres closing ")){
- Nestedres. dosomework (RES2 );
- }
- } Catch (exception e ){
- System. Out. println ("exception:" +
- E. getmessage () + "thrown by:" + E. getclass (). getsimplename ());
- }
- }
- }
public class TryWithRes { public static void main(String[] args) { try(NewResource res = new NewResource("Res1 closing"); NewResource res2 = new NewResource("Res2 closing")){ try(NewResource nestedRes = new NewResource("Nestedres closing")){ nestedRes.doSomeWork(res2); } } catch(Exception e){ System.out.println("Exception: "+ e.getMessage()+" Thrown by: "+e.getClass().getSimpleName()); } }}
Output:
Wow res getting res to do work
Nestedres closing
RES2 closing
RES1 closing
Exception: exception thrown while doing some work thrown by: interval tiona
We can see that the declared resources are closed first, and the previous exceptions are all supressed. You can also use E. getsuppressed () to put all the blocked exceptions, for example
Java code
- Public class trywithres {
- Public static void main (string [] ARGs ){
- Try (newresource res = new newresource ("RES1 closing ");
- Newresource RES2 = new newresource ("RES2 closing ")){
- Try (newresource nestedres = new newresource ("nestedres closing ")){
- Nestedres. dosomework (RES2 );
- }
- } Catch (exception e ){
- System. Out. println ("exception:" +
- E. getmessage () + "thrown by:" + E. getclass (). getsimplename ());
- If (E. getsuppressed ()! = NULL ){
- For (throwable T: E. getsuppressed ()){
- System. Out. println (T. getmessage () +
- "Class:" + T. getclass (). getsimplename ());
- }
- }
- }
- }
- }
public class TryWithRes { public static void main(String[] args) { try(NewResource res = new NewResource("Res1 closing"); NewResource res2 = new NewResource("Res2 closing")){ try(NewResource nestedRes = new NewResource("Nestedres closing")){ nestedRes.doSomeWork(res2); } } catch(Exception e){ System.out.println("Exception: "+ e.getMessage()+" Thrown by: "+e.getClass().getSimpleName()); if (e.getSuppressed() != null){ for (Throwable t : e.getSuppressed()){ System.out.println(t.getMessage()+ " Class: "+t.getClass().getSimpleName()); } } } }}
Output:
Wow res getting res to do work
Nestedres closing
RES2 closing
RES1 closing
Exception: exception thrown while doing some work thrown by: interval tiona
Exception thrown while closing class: exceptionb
Exception thrown while closing class: exceptionb
Exception thrown while closing class: exceptionb