A try-catch statement is a method of catching or throwing an error in the code without disrupting other portions of the code, it also allows you to define a specific exception(the type of error thrown):
try {
//here you put code that will "try" to be ran and "Caught" if an error occurs.
} catch (Exception e) {
e.printStackTrace(); // this will print the source of the error and all previous methods called to the line of the error.
}
There are a few instances where a try-catch is required, such as reading/writing to a file.
Example:
try {
String sItemId = params[0]; // a string array of parameters
int itemId = Integer.valuOf(sItemId); // this is what we are catching the exception for (what if sItemId equals "345s"? 's' cannot be parsed to an integer object!
//blah
} catch (NumberFormatException e) {// here we catch the error with as specific exception
e.printStackTrace();
}
Enums are a way of holding constant values. The advantage to enums over just many final primitive fields is that it can act as an encapsulated object(the data within the enum cannot be changed or edited, but the values can be accessed and the enum can be treated like an object). For example:
public enum Food {
SHRIMP(123,5),
LOBSTER(456,20);
int itemId;
int heal;
Food(int itemId, int heal) {
this.itemId = itemId;
this.heal = heal;
}
public int getItemId() {
return itemId;
}
public int getHealValue() {
return heal;
}
public static Food getFoodForItem(int itemId) {
for(Food food : Food.values())
if(food.getItemId() == itemId)
return food;
return null;
}
}
You can reference the enum by the following:
public void eat(Food food) {
player.heal(food.getHealValue());
}
or
public void eat(int itemId) {
Food food = Food.getFoodForItem(itemId);
if(food != null)
player.heal(food.getHealValue());
}
or
if(food == Food.LOBSTER) {
//blah
}
Enums can be a lot more powerful than this example, but since you're just starting this is enough
They can also be a lot more simple, you could just use it as a reference:
public enum CombatType{
MELEE,
RANGE,
MAGIC;
}
Use:
CombatType currentType = CombatType.MELEE;
//blah...
switch(currentType) {
case CombatType.MELEE:
//blah: fight with sword
break;
case CombatType.RANGE:
//blah: fight with crossbow
break;
case CombatType.MAGIC:
//blah: fight with staff
break;
}
EDIT: added example for try-catch, if you have any other questions pm me or ask here ;p
BTW: I made a 5 on AP comp science(assuming that's what you're in when you said "computer science ap"), and I'll tell you: Neither enums or try-catch are on the exam. You're teacher isn't responding to you because IF you use something on the exam that is not taught in the class, you will not get full credit(on the free response for example). Just a heads up