|
||
| The Following User Says Thank You to helloworld922 For This Useful Post: | ||
Json (07-10-2009) | ||
|
||||
|
To clarify some points above I here have two classes which will look really stupid.
Both of them are valid syntax and they show that you can use certain symbols as variable names as well as class names. It also shows you that you can create a final instance member without setting it straight away, but you HAVE to set it in the constructor or in an instance block but like above, you can only set it ONCE. Now for the freaky stuff. CAUTION: This is not best practise, DO NOT do this when programming Java Code
public class _ {
public int _ = 0;
public final int __;
public _() {
this._++;
this.__ = 5;
}
public static void main(final String... arguments) {
final _ _ = new _();
System.out.println("_ = " + _._);
}
}
Java Code
public class $ {
public final int £;
public int $ = 0;
public $() {
this.$++;
this.£ = 5;
}
public static void main(String... arguments) {
final $ $ = new $();
System.out.println("$ = " + $.$);
}
}
![]() Happy coding! Enjoy! // Json |
|
||||
|
Exceptions
Exceptions are special things that interrupt normal program flow. They're used to create robust methods that follow their contracts exactly. Here's a small example: Java Code
// * note: this code won't compile as-is, but will demonstrate our purposes here
public int doOperation(int a, int b, char operator)
{
if (operator == '+')
{
return a+b;
}
else if (operator == '-')
{
return a-b;
}
else if (operator == '*')
{
return a*b;
}
else if (operator == '/')
{
return a/b;
}
}
The answer is to use exceptions. Exceptions are thrown, and then must be caught. There are two types of exceptions: unchecked, or checked. The difference is who has to catch the exception. Unchecked exceptions don't have to be caught, and checked ones do. Note: the JVM will end up catching all exceptions that get passed to it, but this is quite nasty and means your program is completely terminated. To throw an exception: Java Code
throw new Exception(); Note: the Exception class is a checked exception, so it must be caught. If you want an unchecked exception, inherit from the RuntimeException class. So how do you catch an exception? The answer is with try/catch blocks. Java Code
try
{
// code
}
catch(Exception e)
{
// stuff to do if an Exception is caught
}
Java Code
try
{
// code
}
catch(NullPointerException e)
{
// stuff to do if a NullPointerException is caught
}
So what if you don't want your method to catch the exception, but throw it up to the caller? For unchecked exceptions, you don't have to do anything, but for checked exceptions you have to declare in the method header that your method will throw certain types of exceptions Java Code
// note: InvalidOperatorException isn't a default Java Exception, but here pretend it extends the Exception class.
//There is class called UnsupportedOperationException, but to demonstrate our purposes, I chose not to use it here
public int doOperation(int a, int b, char operator) throws InvalidOperatorException
{
...
}
Java Code
// will do the exact same thing as the above code
public int doOperation(int a, int b, char operator) throws InvalidOperatorException, ArithmeticException
{
...
}
__________________
ASCII a question .. Get an ANSI Please surround your code with [highlight=Java]code goes here[/highlight]. Last edited by helloworld922; 13-01-2010 at 01:21 AM. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
|
100 most searched terms
Search Cloud
|
| 2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? |