I have a simple setter method for a Java property and null is not appropriate for this particular property. I have always been torn, in this situation: should I throw an IllegalArgumentException, or a NullPointerException? From the javadocs, both seem appropriate. Is there some kind of understood standard? Or is this just one of those things that you should do whatever you prefer and both i...
If I have a set method in which I want to modify some values,
if an user enter wrong values which is the best exception to throw to indicate that failure?
public void setSomething(int d) throws ....
{
if (d < 10 && d >= 0)
{
// ok do something
}
else throw new ... // throw some exception
}
Are there any guidelines on exception propagation in Java?
When do you add an exception to the method signature?
For example: if an exception is only thrown when an essential program resource is missing, and can only be handled at the top level, do I propagate it through all methods using this exception through all the methods using the erring method?
Are there any good practices? Any bad pra...
I have a function which calculates the mean of a list passed as an argument. I would like to know which of Java exception should I throw when I try to compute the mean of a list of size 0.
public double mean (MyLinkedList<? extends Number> list)
{
if (list.isEmpty())
throw new ????????; //If I am not mistaken Java has some defined exception for this case
//code goes here...
I want to ask why we don't have to add try-catch block to a RuntimeException while we should do that with other exceptions?
I mean like :
public class Main {
public static void main(String[] args) {
throw new RuntimeException();
}
}
Edit :
when I say : throw new RuntimeException(); it is so clear that there is an exception will happen ,so why the compiler doesn't forbid that ?
I'm converting some C# code to Java and I need to include an exception that is similar to C#'s InvalidOperationException. Does such a thing exist? Also is there a list of equivalent exception types in the two languages? Thanks.
I think in my particular case IllegalStateException is most appropriate. Thanks for all the responses.
most of the time i will use exception to check for condition in my code, i wonder when is appropriate time to use assertion
for instance,
Group group=null;
try{
group = service().getGroup("abc");
}catch(Exception e){
//i dont log error because i know whenever error occur mean group not found
}
if(group !=null)
{
//do something
}
1.can comment on this code and how assertion fit in here ...
Take this method
/**
* @return List of group IDs the person belongs to
*
*/
public List<String> getGroups() {
if (this.getId().equals("")) return null;
}
I would like to throw exception instead returning null, what's the exception to throw when an important parameter/dependency has not been set?
It seems like it is not a good idea to catch a nullpointerexception. If that's the case, why is it thrown by methods? Should it just be caught with Exception?
Also, if I encounter a parameter which is null (can either be primitive type like string or a class with fields), how should I handle it? (assumingly not throw npe)?
Thanks
Is there any way to throw multiple exception in java.
Thanks
I want to throw a runtime exception in case my class invariants are invalidated. Since this is a programming error (similar to a NullPointerException), clients should not catch that exception.
Should the exception class be declared private or public (or something else)?
class Foo
{
// ...
private static class InvariantsViolated
{
// ...
}
}
Are there any guidelines ...
I am trying to create a Response class in Java which has a method
void setResponse(String response);
Different response subclasses will have different requirements for the response. The string that is passed to the function is received from the user.
What is the correct way of handling a wrong response?
Should the function throw an exception like IllegalResponseException
Should the ...
Suppose I have the following method:
public void change(Map<String, String> map)
I would like to throw an exception if map is null and make a defensive copy if it's not.
Would this be preferred:
public void change(Map<String, String> map)
{
Map<String, String> temp = null;
synchronized (map) {
if (map == null)
throw new NullPointerExce...
Consider this simple program. It has two files:
Vehicle.java:
class Vehicle {
private int speed = 0;
private int maxSpeed = 100;
public int getSpeed()
{
return speed;
}
public int getMaxSpeed()
{
return maxSpeed;
}
public void speedUp(int increment)
{
if(speed + increment > maxSpeed){
// throw exception
...
I just started to learn Java. There is a course in MIT and there is an assignment to write a class for pay calculation. Part of the task is:
The base pay must not be less than the minimum wage ($8.00 an hour). If it is, print an error.
If the number of hours is greater than 60, print an error message.
The code I have written is as follows:
public class fooCorporation {
privat...
I am following this great discussion at SO, titled: The case against checked exceptions , but I am unable to follow where exactly RuntimeException should be used and how it is different from normal Exceptions and its subclasses. Googling gave me a complex answer, that is, it should be used to deal with programming logic errors and should be thrown when no Exception should normally occur, such a...
From a class extending java.beans.PropertyEditorSupport :
/**
* Sets the property value by parsing a given String. May raise
* java.lang.IllegalArgumentException if either the String is
* badly formatted or if this kind of property can't be expressed
* as text.
*
* @param text The string to be parsed.
*/
public void setAsText(String name) {
try {
asEnum(name);
} catch (InvalidEnu...
I am not good at handling exceptions, so I need a hint at this occasion:
I want to put arrays in a collection (ArrayList) all of which should be of the same length. Otherwise errors emerge in computations. When an array of not desired length is to be inserted in the ArrayList, I would like to throw an exception with a message. What kind of exception is suitable for this occasion?
What worries...
I am new to JPA/Hibernate. Currently using EJB3, Hibernate/JPA. I have an inheritacnce structure as follows..
@Entity
@DiscriminatorColumn(name = "form_type")
@Inheritance(strategy = InheritanceType.JOINED)
@GenericGenerator(name = "FORMS_SEQ", strategy = "sequence-identity", parameters = @Parameter(name = "sequence", value = "FORMS_SEQ"))
@Table(name = "Forms")
public abstract class Form{
...
I wrote a code which checks all kinds of conditions.
If it meets the condition it does what it is supposed to, otherwise I want it to throw an
exception.
Is there any special syntax for that? Otherwise the compiler wants me to return any array,
which I don't want to, due to the pre-condition.
Here is part of my code:
public static int [] code(int[]arr){
if ((arr!=null)&&(chack4...
Let's assume I'm a complete lazy bum and I don't want to invest the several dozen keystrokes needed for my own Exception class (it's not utterly important which gets used, really). However, to pretend I'm following good practices here, I want a pre-existing one that best fits my situation.
Problem: I need to throw an exception when my class's constructor receives an object in its parameters t...