September 04, 2010, 03:13:59 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: whether to use try/catch block or throws keyword  (Read 803 times)
0 Members and 1 Guest are viewing this topic.
anandjain1984
Junior Member
*

Reputation: 0
Offline Offline
Posts: 3
Referrals: 0

Awards
« on: December 14, 2009, 06:40:09 AM »

if we are writing a A code, then how to decide whether to handle the exception using try/catch block or handle the exception using throws keyword
Logged
Javaforums.net :: a community about Java software development.
« on: December 14, 2009, 06:40:09 AM »

 Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« Reply #1 on: December 14, 2009, 10:14:32 AM »

please explain with more details.

You can catch exceptions like this:

try{}
catch(YourException e){}
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
trey
Member
*

Reputation: 0
Offline Offline
Posts: 16
Referrals: 0

Awards
« Reply #2 on: December 14, 2009, 04:19:55 PM »

I think you are asking when you should explicitly enforce try/catch using the throws keyword, or just handle the exceptions within the method itself, right?

Code:
private void xmpl() throws NumberFormatException {
     Integer.parseInt("apple");
}

Code:
private void xmpl() {
    try {
        Integer.parseInt("apple");
    } catch (NumberFormatException e) {
    }
}

When you use 'throws', it forces any call to that method to be within a try/catch block, which can be a good thing, especially if your code is going to be used by others. Meaning that if you where write some sort of library that handles something that is very error prone, then using throws and forcing the user to handle exceptions may be a good idea, as opposed to just handling the exceptions within the methods themselves. Make sense? Method's that throw exceptions don't have to be surrounded in a try/catch block, (for example, using 'throw'), but if you add the 'throws' keyword in the method declaration, you are forced to enclose the call in a try/catch block, and if you don't you will receive an error at compile time. Remember that compile time errors are always better than runtime errors wink.
Logged
Javaforums.net :: a community about Java software development.
   

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.294 seconds with 35 queries.

Google visited last this page August 30, 2010, 12:18:12 AM