ExceptionHandeling
This commit is contained in:
@ -8,6 +8,30 @@ public class Main {
|
|||||||
SERGEANT,
|
SERGEANT,
|
||||||
CAPTAIN
|
CAPTAIN
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int div(int a, int b) throws ArithmeticException {
|
||||||
|
if(b == 0) {
|
||||||
|
throw new ArithmeticException("Division by Zero");
|
||||||
|
} else {
|
||||||
|
return a / b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class FirstException extends Exception { }
|
||||||
|
static class SecondException extends Exception { }
|
||||||
|
|
||||||
|
public void rethrowException(String exceptionName) throws Exception {
|
||||||
|
try {
|
||||||
|
if (exceptionName.equals("First")) {
|
||||||
|
throw new FirstException();
|
||||||
|
} else {
|
||||||
|
throw new SecondException();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Hello world!");
|
System.out.println("Hello world!");
|
||||||
|
|
||||||
@ -68,6 +92,16 @@ public class Main {
|
|||||||
|
|
||||||
// Once you locate the package you want to use, you need to import it into your code.
|
// Once you locate the package you want to use, you need to import it into your code.
|
||||||
|
|
||||||
|
|
||||||
|
//Exception Handeling
|
||||||
|
try{
|
||||||
|
int an = 0 / 0;
|
||||||
|
}catch (Exception e){
|
||||||
|
System.out.println("Here was an error: " + e);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(div(42, 0));
|
||||||
|
|
||||||
|
//https://docs.oracle.com/javase/8/docs/technotes/guides/language/catch-multiple.html
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user