Results 1 to 5 of 5

Thread: How to Throw Exceptions in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    222

    How to Throw Exceptions in Java?

    Hello folks,
    I have recently started doing the coding in Java, so I don't have much idea about it. Now I started with the topic of an Exceptions. I just came to know that why we use an exceptions but I don't know how to throw it. Please tell me how to Throw Exceptions in Java? Please provide any coding so that I can easily understand.!!
    Just a reply to say thank you for these links and posts I have a lot to read and learn now!



  2. #2
    Join Date
    Jul 2006
    Posts
    286

    Re: How to Throw Exceptions in Java?

    Some code somewhere must throw before you can catch an exception. All the classes of an exceptions are derived from the Throwable class. You can also create your own exception classes to represent problems that can occur within the classes you write. Before catching an exception it is must to be thrown first. This means that there should be a code somewhere in the program that could catch the exception.
    IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....

  3. #3
    Join Date
    Jul 2006
    Posts
    442

    Re: How to Throw Exceptions in Java?

    For throwing an exceptions you did not have to work much. Actually throwing an exceptions is very simple to do. Since you are new you may find difficult but when you go on practicing you will find it very easy. You have to use throw statement to throw an exception or simply use the throw keyword with an object reference to throw an exception. Throwable objects are instances of any subclass of the Throwable class. The following is an example of the throw statement :
    Code:
    throw someThrowableObject;
    "When they give you ruled paper, write the other way..." J.R.J.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to Throw Exceptions in Java?

    You need a single argument for using the throw statement, which is a throwable object. As explained to you before the Throwable objects are instances of any subclass of the Throwable class. The following is the example of the same :
    Code:
    throw new VeryFastException();
    You will have to keep in mind that the reference should be of type Throwable or one of its subclasses.

  5. #5
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to Throw Exceptions in Java?

    I have provided you with a sample of the coding that may help you. The following is the sample for the same :
    Code:
    class ExceptionDemo extends Exception {
    public ExceptionDemo(String msg){
    super(msg);
    }
    }
    
    public class Test {
    
    static int  divide(int first,int second) throws ExceptionDemo{ 
      if(second==0) 
      throw new ExceptionDemo("can't be divided by zero");
    return first/second;
     }
    
     public static void main(String[] args) {
      try {
    System.out.println(divide(4,0));
      }
    catch (ExceptionDemo excp) {
    excp.printStackTrace();
      }
      }
    }

Similar Threads

  1. No New HD channels ,Should I throw my Tata Sky DTH?
    By DIMINUTIVE! in forum India BroadBand
    Replies: 3
    Last Post: 18-11-2011, 10:32 PM
  2. What are the different built-in Exceptions available in java?
    By ScarFace 01 in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 09:06 AM
  3. Replies: 5
    Last Post: 18-01-2010, 05:41 PM
  4. What are user defined exceptions in Java?
    By Migueel in forum Software Development
    Replies: 5
    Last Post: 30-11-2009, 07:27 PM
  5. Difference between throw and throws in Java Exception Handling
    By KALYAN23 in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 11:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,567,500.86719 seconds with 17 queries