aspose file tools*
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 Java 8 in Action this week in the Java 8 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: throw Exception
 
Similar Threads
What are the ways to Create UserException Classes
blunder in Mughal and Rasmussen??
UserDefinedException handling
code
Handle and Declare rule - Ducking exceptions