aspose file tools*
The moose likes Java in General and the fly likes User defined unchecked exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "User defined unchecked exceptions" Watch "User defined unchecked exceptions" New topic
Author

User defined unchecked exceptions

sita raman subra
Ranch Hand

Joined: Apr 09, 2007
Posts: 47
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

Joined: Oct 13, 2005
Posts: 38756
    
  23
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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sita raman subra
Ranch Hand

Joined: Apr 09, 2007
Posts: 47
Hi ,

Thanks for immediate reply.

Prime,
Am not able to get you?

Thanks and Regards,
Sita
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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

Joined: Apr 09, 2007
Posts: 47
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

Joined: Oct 13, 2005
Posts: 38756
    
  23
No. You ought to know how to call superclass constructors with super.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
And exception constructors should never do console output.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: User defined unchecked exceptions