Created
July 8, 2013 11:27
-
-
Save gjb2048/5948009 to your computer and use it in GitHub Desktop.
Custom exception example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Custom exception example. | |
* © G J Barnard 2013 - Attribution-NonCommercial-ShareAlike 3.0 Unported - http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB. | |
*/ | |
package myexception; | |
/** | |
* Demonstrate how custom exceptions can be defined and employed. | |
*/ | |
public class ExceptionTest | |
{ | |
private int x; | |
/** | |
* @param args - Not used. | |
*/ | |
public static void main(String[] args) | |
{ | |
ExceptionTest us = new ExceptionTest(); | |
/* | |
* The purpose here is to demonstrate how you can define your own exceptions for invalid program situations where | |
* perhaps you have not performed validation. By specifying that a certain input can be between 1 and 100 then | |
* any number outside of that range is invalid and should be treated as such. Exceptions do not have to be reported | |
* to the user or with the full stack trace. They can just be handled internally. The beauty of using them comes | |
* with logging where full stack traces can assist you in fixing a sitaution that a user has encountered. | |
*/ | |
int guesses[] = | |
{ | |
-2, 1, 100, 105, 42 | |
}; | |
System.out.println("Guessing the value of X..."); | |
for (int i = 0; i < guesses.length; i++) | |
{ | |
System.out.print("Trying: " + guesses[i]); | |
try // Only put in the try block what you need. | |
{ | |
if (us.guessX(guesses[i])) | |
{ | |
System.out.println(" - Correct"); | |
} | |
else | |
{ | |
System.out.println(" - Incorrect"); | |
} | |
} | |
catch (MyException ex) | |
{ | |
System.out.println(); | |
// Note that we just report here, there is no re-throw and program execution continues - there is no crash. | |
System.err.println(ex.getReason()); | |
// Here you show a single message to the user as above and log the stack trace for them to send to you. | |
System.err.println("Stack trace..."); | |
ex.printStackTrace(System.err); | |
} | |
} | |
} | |
/** | |
* Set the value to guess. | |
*/ | |
public ExceptionTest() | |
{ | |
x = 42; | |
} | |
/** | |
* Checks to see if the guess is correct. | |
* | |
* @param guess The guess. | |
* @return If the guess is correct. | |
* @throws MyException If the guess is outside the valid range. | |
*/ | |
public boolean guessX(int guess) throws MyException | |
{ | |
boolean retr = false; | |
if ((guess < 1) || (guess > 100)) | |
{ | |
throw new MyException("Guess " + guess + " must be between 1 and 100."); | |
} | |
else if (guess == x) | |
{ | |
retr = true; | |
} | |
// I dislike methods that return all over the place, better to have a single clean point of return. Except the exception! | |
return retr; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Custom exception example. | |
* © G J Barnard 2013 - Attribution-NonCommercial-ShareAlike 3.0 Unported - http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB. | |
*/ | |
package myexception; | |
/** | |
* Define your own exception. | |
*/ | |
public class MyException extends Throwable | |
{ | |
private static final long serialVersionUID = -7131266248952296430L; | |
private String reason; | |
/** | |
* Constructor | |
* @param reason The reason for the exception. | |
*/ | |
public MyException(String reason) | |
{ | |
super("MyException - " + reason); | |
this.reason = reason; | |
} | |
/** | |
* Prevent use of no parameter constructor. | |
*/ | |
private MyException() | |
{ | |
} | |
/** | |
* Gets the reason for the exception. | |
* @return String | |
*/ | |
public String getReason() | |
{ | |
return "MyException::getReason() - " + reason; // Prefixed with the method name to understand how printStackTrace() works. | |
} | |
/** | |
* Override default method. | |
* @return String | |
*/ | |
@Override | |
public String toString() | |
{ | |
return "MyException::toString() - " + reason; // Prefixed with the method name to understand how printStackTrace() works. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment