File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I throw an exception on purpose?

 
Reggie McDougal
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to check for a null value in my condtion I want to deliberatly throw an exception.

Can I use a method to throw an exception?
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You throw an exception by using the throw keyword and creating an instance of some class that extends Exception:


HTH

Layne
 
Sunil Manheri
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create your own Exception class and throw that excaption.

public class MyException extends Exception {

public MyException(){
super("Exception Occured!");
}

}

Then throw this Exception as:

throw new MyException();
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To learn the basics of exception handling, I recommend reading
  • chapter 9 of Bruce Eckel's Thinking In Java Book
  • chapter 9 of David J. Eck's Introduction to Programming Using Java
  • chapters 80 & 81 of Bradley Kjell's Introduction to Computer Science using Java
  • Dick Baldwin's The Essence of OOP using Java, Exception Handling Article and
  • The Handling Errors with Exceptions Lesson of Sun's Java Tutorial
  • Then, when you're ready to have some fun, take a look at this past JavaRanch conversation as well as http://c2.com/cgi/wiki?IlluminateTheMainline
     
    Mahesh Bhatt
    Ranch Hand
    Posts: 88
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi there,

    Just have to say one thing.Dirk,That list of links was great.

    regards
    Prashant
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic