Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

User defined unchecked exceptions

 
sita raman subra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please advice whether their is any way to create USER DEFINED unchecked exception?

Method 1 :

class A extends RuntimeException{
public void m1(){
}
}


Any other Methods?
[ January 07, 2008: Message edited by: David O'Meara ]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Same way you create any class: make a subclass of another class.
Find RuntimeException or any of its subclasses, and make a subclass of it. You usually need to create four constructors to match the superclass constructor, and nothing else.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
You usually need to create four constructors to match the superclass constructor, and nothing else.

The four taking the following parameters:
- nothing
- String message
- Throwable cause
- String message, Throwable cause

Usually you just pass these values to the super class' constructor. Now if the super class does not have a constructor that takes a cause, you can always set it using the initCause(Throwable cause) method.
 
sita raman subra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Thanks for immediate reply.

Prime,
Am not able to get you?

Thanks and Regards,
Sita
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't usually give out simple answers or solutions to questions. We presumed that you know how to create new classes and how to give them constructors. Go and look in the API for RuntimeException and find its constructors. Look at their details, then set up new constructors as Rob Rpime has already told you.

Look through the subclasses of RuntimeException and see whether there is another type of Exception which is appropriate as a superclass for your Exception.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to clarify my last statement: there are exception classes (ArithmeticException is one, IOException was one before Java 6) that do not have constructors that take a Throwable cause - only the other two constructors are present.

The initCause method allows you to pass a constructor in your own method and still be able to attach it to the exception, even though you can't by passing it to the super class' constructor.
 
sita raman subra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you mean
public class UnCheckedExceptionExam extends RuntimeException{

public UnCheckedExceptionExam(){
System.out.println("No argument");
}
public UnCheckedExceptionExam(String message){
System.out.println(message);
System.out.println(initCause(new Throwable("define exception")));
}
public UnCheckedExceptionExam(Throwable cause){
System.out.println(cause.getCause());
}
public UnCheckedExceptionExam(String message,Throwable cause){
System.out.println(message);
System.out.println(cause.getCause());

}
}

Regards,
Sita Raman
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You ought to know how to call superclass constructors with super.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And exception constructors should never do console output.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic