Results 1 to 3 of 3

Thread: Exceptions

  1. #1
    Maya is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Exceptions

    Hello,

    I am new to Java and learning it. While reading about exception I got a doubt. In what difference conditions do we use throwable and try catch blocks? What does trowing an exception mean? While reading it, I felt that both of them are used for same purpose, handling exceptions, then in what way are these two different?
    I also have a doubt in Interfaces. When no methods are implemented in interface, why do we need to implement them. Can't we just declare and implement them directly in the methods?

    Thanks,
    Maya
    Last edited by Maya; 05-23-2011 at 10:35 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You use the throws clause to actually throw the exception, but try catch handles the thrown exception.

    For example, you could write a method that looks like this
    Java Code:
    public void doSomething(){
      Scanner s = new Scanner(System.in);
      System.out.println("Please enter a number less than 100");
      int n = s.nextInt();
      if(n > 100){
        throw new IllegalArgumentException(n + " Is greater than 100");
      }
    }
    This just will cause an exception to be thrown when something incorrect is done, the try catch statement will allow you to handle this exception. Any method that is said to be risky(and thus is wrapped in a try catch block) throws exceptions inside the method.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Quote Originally Posted by Maya View Post
    When no methods are implemented in interface, why do we need to implement them. Can't we just declare and implement them directly in the methods?
    The interfaces with no methods inside it are known as Marker [or Tagged] interfaces. They provide a special purpose to the classes that implement them. We should implement them to achieve that particular functionality.

    Have a look here for details : Marker Interface In Java

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Exceptions?
    By linc186 in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 09:03 AM
  2. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 02:44 PM
  3. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 10:06 PM
  4. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 09:38 AM
  5. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •