ExceptionHandeling

This commit is contained in:
2024-10-06 19:42:41 +02:00
parent 092709bb24
commit 51cd62c917
5 changed files with 34 additions and 0 deletions

View File

@ -8,6 +8,30 @@ public class Main {
SERGEANT,
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) {
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.
//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
}
}