This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes throw  Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "throw  Exception" Watch "throw  Exception" New topic
Author

throw Exception

Arun Maalik
Ranch Hand

Joined: Oct 25, 2005
Posts: 216
class UserException extends Exception{
UserException(){
System.out.println("It is in UserException ");
}
}


public class first{
public static void main(String[] args){
try{
throw new UserException();
}

catch(UserException e){
System.out.println(e);
}
}
}

In the above code sir what is need to extends it with Exception class. I think throw statment is creating an object of Exception class. M i right sir?

with regard

Arun kumar maalik
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
The new after throw is what creates the object.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Arun Maalik:
class UserException extends Exception{
UserException(){
System.out.println("It is in UserException ");
}
}


public class first{
public static void main(String[] args){
try{
throw new UserException();
}

catch(UserException e){
System.out.println(e);
}
}
}

In the above code sir what is need to extends it with Exception class. I think throw statment is creating an object of Exception class. M i right sir?

with regard

Arun kumar maalik


You can throw only if the it is the instance of Throwable, and if you don't extend it to exception class or throwable class how can you able to throw any exception.

That's why it is needed to extend any Throwable class or any sub class of Throwable to throw an user defined exception.

It is the same logic is as we have to either extend a Thread class or implement Runnable interface to create or run a thread.

I hope it helps you.


The Best way to predict your future is to create it - Every great individual common man
 
 
subject: throw Exception