茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1675430 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_107.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

爪哇咖啡屋 : [轉貼]Java How to Throw Exceptions

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]How to throw a general exception in Java?
How to throw a general exception in Java?

Consider this simple program. The program 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
        }else{
            speed += increment;
        }
    }

    public void speedDown(int decrement)
    {
        if(speed - decrement < 0){
            // throw exception
        }else{
            speed -= decrement;
        }
    }
}

And HelloWorld.java:
public class HelloWorld {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Vehicle v1 = new Vehicle();
        Vehicle v2 = new Vehicle();

            // do something

            // print something useful, TODO
        System.out.println(v1.getSpeed());
    }

}

As you can see in the first class, I have added a comment ("// throw exception") where I would like to throw an exception. Do I have to define my own class for exceptions or is there some general exception class in Java I can use?
java exception



You could create your own Exception class:
public class InvalidSpeedException extends Exception {

  public InvalidSpeedException(String message){
     super(message);
  }

}

In your code:
throw new InvalidSpeedException("TOO HIGH");




You could use IllegalArgumentException:
public void speedDown(int decrement)
{
    if(speed - decrement < 0){
        throw new IllegalArgumentException("Final speed can not be less than zero");
    }else{
        speed -= decrement;
    }
}




Well there are lots of exceptions to throw but here is how you throw an exception:
throw new IllegalArgumentException("INVALID");

Also yes you can create your own custom exceptions.

Edit: Also note about exceptions. When you throw an exception (like above) and you catch the exception: the String that you supply in the exception can be accessed throw the getMessage() method.
try{
    methodThatThrowsException();
}catch(IllegalArgumentException e)
{
  e.getMessage();
}




It really depends on what you want to do with that exception after you catch it. If you need to differentiate your exception then you have to create your custom Exception. Otherwise you could just throw new Exception("message goes here");



The simplest way to do it would be something like:
throw new java.lang.Exception();

However the following lines would be unreachable in your code. So, we have two ways:

原文出處:How to throw a general exception in Java? - Stack Overflow
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|