0

i have following code and i like to get exception message using finally as by using catch i can easily get by its arg.but as much i know i am not able in get exception message using finally.

try {
 MyClass obj=new MyClass();
 obj.strProName = jobj1.getString("productname");
 obj.strPrice = jobj1.getString("price");
 obj.strCurrency = jobj1.getString("currency");
 obj.strSalePrice = jobj1.getString("saleprice");
 obj.strStoreName = jobj1.getString("storename");

//arrayList.add(obj);
throw new Exception("Exception Reason!");

}
finally{
 //want to get that exception message here without using catch or can see how finally catching here the exception
}
Saurabh Android
  • 668
  • 8
  • 15

5 Answers5

6

Unlike catch block ,finally block does'nt receive any exception instance

So,The answer is No from my side.

What I mean is to print the message,You need Exception instance.

As per docs (jls-14.2)

A block is a sequence of statements, local class declarations, and local variable declaration statements within braces.

So outside of catch block catch(Exception e) {} you cannot access it (e).

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    @sureshatta your one line is worth my 3 lines :) upvote from me – Prasad Kharkar Aug 22 '13 at 11:46
  • 1
    i know this but now also my question remains unanswered. as i want to know only this thing how new Exception("Message") is being resolved by finally.as their must be any one string argument method need to be invoked but catch is not present with that but catch is..can u suggest something on this – Saurabh Android Aug 22 '13 at 11:53
3

but as much i know i am not able in get exception message using finally.

That's correct, to catch an excpetion you, well... have to use a catch clause.

You could however store the message in a variable (in the catch clause) and use that variable in the finally clause later.

Puce
  • 37,247
  • 13
  • 80
  • 152
2

finally does not catch the exception. You can catch exception only in catch block. The purpose of finally block is to execute in both cases, i.e. it will execute irrespective of exception being occured or not.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
1

Finally does not catch exception, it is simply a thing you can use to always do something, even if there is no error and the catch is never called for.

try {
 MyClass obj=new MyClass();
 obj.strProName = jobj1.getString("productname");
 obj.strPrice = jobj1.getString("price");
 obj.strCurrency = jobj1.getString("currency");
 obj.strSalePrice = jobj1.getString("saleprice");
 obj.strStoreName = jobj1.getString("storename");
}
//arrayList.add(obj); here you can Catch the exception, meaning it will only show if there is an exception!
catch(Exception e){
 System.out.print(e+"=Exception Reason!");
}
finally{
//Finally is used to do something no matter what. 
//It will do what ever you want it to do, 
//even if the catch is never used. 
//Use catch to show exception, 
//finally to close possible connections to db etc.
}
ghoulfolk
  • 326
  • 7
  • 17
  • 1
    this answer is not having any new here as i also mentioned the similar in my question too .i want to know how to get message using finally without catch as thier must be any Exception(String) cons will be invoked but by using we are not supplying that in code.so how our code or how finally do that.i want to know only this thing..can u put some light on this thought – Saurabh Android Aug 22 '13 at 11:59
-1

Try this

try {
       .....
       throw new Exception("Exception Reason!");
    }
catch(Exception e){
     msg=e.getMessage();
finally{
//USE String msg here.
}
Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
  • 1
    i mentioned this in my question too but i want to get message using finally without catch as u now finally is a alter of catch that. – Saurabh Android Aug 22 '13 at 11:51
  • Yeah. But you can't catch exception using finally block. I thought you might want the message in finally block thats why i suggest above option. – Vimal Bera Aug 22 '13 at 11:57
  • i want to know how string arg constructor of Exception class is managed by thrown exception for case when we are not using catch.i have this confusion only. can u suggest something on this – Saurabh Android Aug 22 '13 at 12:01
  • Try to debug your program. You will get to know the flow of the Exception. There is a constructor defined for `Exception(String msg)` which will ultimately save your msg in `detailMessage` field. – Vimal Bera Aug 22 '13 at 12:11
  • as i am throwing a new obj of Exception(String) in my code so if Exception(String msg) is not available then there will be compile time error in my code. – Saurabh Android Aug 22 '13 at 12:16
  • Yeah. But Exception(String) is already defined in class `Exception`. Refer [this](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Exception.html) documentation from oracle. – Vimal Bera Aug 22 '13 at 12:19