• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to catch the exception?  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


explain me the way to handle the exception that has been in the catch block{b.rt()} .
 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bairava,

I noticed, that you're not trying to follow a good practice of code indentation (not a first time).
Get rid of this bad habit. As it is not nice to read your code (at least your pasted code).
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by providing code correctly indented so we can actually read it.

You decide how you ought to handle that Exception. If an Exception occurs there, what does it mean? How do you intend to recover from it? And also explain why code like that embodies very poor practice.
 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:

explain me the way to handle the exception that has been in the catch block{b.rt()} .


See, in this way, you, and especially everyone else can read your code the way easier.
This is the common way to respect everyone who is trying to help you.

 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hay Surya ,
You are calling a method that is throwing a runtime exception in the catch block !!! It should give compile time exception .


explain me the way to handle the exception that has been in the catch block{b.rt()} .


re throw it from catch or surround it by try / catch :






Thanks ,
Satya
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Majumder wrote:You are calling a method that is throwing a runtime exception in the catch block !!! It should give compile time exception .

Throwing an exception in a catch block is not a problem. That will not cause a compile error.

(Note that there is no such thing as a "compile time exception").
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sugest me how to handle this code?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:sugest me how to handle this code?

You can just put the call b.rt(); in a try-catch block. There's nothing special about it just because you happen to do this call inside a catch block.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
S Majumder wrote:You are calling a method that is throwing a runtime exception in the catch block !!! It should give compile time exception .

Throwing an exception in a catch block is not a problem. That will not cause a compile error.

(Note that there is no such thing as a "compile time exception").


Jesper ,

Ya that should be checked exception not compile time exception ...
I tried to run that code given by surya and what I have got that is mentioned on the below snapshot :

Can you explain why am getting this ?

I guess it is a very basic thing ,, one method is throws an Exception (here rt() throws Exception) , when that method is getting called need to re-throw that exception / catch that exception !!!

Satya
Capture.PNG
[Thumbnail for Capture.PNG]
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you call a function that throws an exception, you must either catch it or throw it too.
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a common real-world everyday use case example:


Note that it's perfectly permissible to have Exceptions in catch and finally clauses and they get dealt with the same way that exceptions are dealt with everywhere else. If the exceptions are caught, they are consumed and execution resumes from the "catch" point onwards. If they are not caught (or caught and re-thrown), then the exception percolates up the call stack.

Ultimately, if an exception isn't caught in application code, the JVM itself catches the exception. Its usual response is to report the exception and stacktrace on system.err and then terminate itself.
 
Tim Cooke
Marshal
Posts: 4037
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya, you've gotten lots and lots of good information in this thread. What do you make of it? Does it all make sense? Is there any part that you don't understand?

You also got a lot of good information in your other thread on a similar topic that you appear to have abandoned. Did you find that useful? There are some open questions there for you too, should you wish to return and continue the discussion.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!