Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throw Exception

 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new after throw is what creates the object.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic