Exception






* Exception - klasinin ozu checked sayilir.

Checked Exception - is caught at compile time and must be handled either by re-throwing or with a 
try-catch block. It is an exception condition within your code's logic and can be recovered or re-tried from.

Unchecked Exception - is runtime exception and is not required to be handled. A runtime exception is a programming error and is fatal. 


Runtime Exception - bu exceptionlari try catch blokuna salmaga ehtiyac yoxdur. Yeni bu exceptionu tutmasaqda olar. Cunki Java qabaqcadan bilirki eger unchecked exception cixsa ne etmeliyem. 
package main;

public class Account {

public Double amount = 0.0;

public void withdraw(Double a) {
if (a <= amount) {
this.amount -= a;
} else {
throw new RuntimeException("Sizin kifayet qeder mebleginiz yoxdur");
}
}

public void deposit(Double a) {
this.amount += a;
}
}
---------------------------------------------------------------------------------------------
Try Catch example:
package main;

public class Main {
public static void main(String[] args) throws Exception {
// try {
// exception();
// } catch (Exception e) {
// System.out.println("Catch bloku");
// }
exception();
System.out.println("Davam");
}

public static void exception() throws Exception {
throw new Exception("Xeta baw verdi");
}
}

-----------------------------------------------------------------------------------------------------------------------------

package main;

public class Main {
public static void main(String[] args) throws Exception {
try {
System.out.println("Hello");
exception();
method1();
} catch (Exception e) {
System.out.println("Catch bloku");
}

System.out.println("Davam");
}

public static void exception() {
throw new RuntimeException("Xeta baw verdi");
}

public static void method1() {
System.out.println("method1");
}
}

---------------------------------------------------------------------------------------------
Try Catch Finally example:
finally - o demekdir ki, ne olursa olsun bu block iwleyecek. finally block bir halda iwlemeye 
biler. System.exit(0);

package main;

public class Main {
public static void main(String[] args) throws Exception {
try {
System.out.println("Hello");
exception();
method1();
} catch (Exception e) {
System.out.println("Catch bloku");
System.exit(0);
} finally {
System.out.println("finally");
}

System.out.println("Davam");
}

public static void exception() {
throw new RuntimeException("Xeta baw verdi");
}

public static void method1() {
System.out.println("method1");
}
}

---------------------------------------------------------------------------------------------
try finally example: Bu zaman exception atilir lakin finally bloku mutleq iwe duwur. 
package main;

public class Main {
public static void main(String[] args) throws Exception {
try {
System.out.println("Hello");
exception();
method1();
// } catch (Exception e) {
// System.out.println("Catch bloku");
// System.exit(0);
} finally {
System.out.println("finally");
}

System.out.println("Davam");
}

public static void exception() {
throw new RuntimeException("Xeta baw verdi");
}

public static void method1() {
System.out.println("method1");
}
}

---------------------------------------------------------------------------------------------
Example1:


---------------------------------------------------------------------------------------------

Oz exception klasimizin yaradilmasi:
package main;

public class MyException extends Exception {

public MyException(String msg) {
super(msg);
}
}
package main;

public class Main2 {
public static void main(String[] args) {
Account account = null;

if (account == null) {
try {
throw new MyException("Null xetasi");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

--------------------------------------------------------------------------------------------

Error
An Error indicates serious problems that a reasonable application should not try to catch.
Errors are the conditions which cannot get recovered by any handling techniques. 
* Error-u try catch blokuna salmaq olar lakin biz hemin errorun tipine gore hereket etmeliyik.

----------------------------------------------------------------------------------------------
* Vacib meqamlardan biri. Diqqetli olun!
package main.exception;

public class MyException1 extends RuntimeException {
public MyException1(String message) {
super(message);
}
}
package main.exception;

public class MyException2 extends Exception {

public MyException2(String message) {
super(message);
}
}

---------------------------------------------------------------------------------------------

package main.error;

public class Main {
public static void main(String[] args) {

}

public static void throwException1() {
System.out.println("I'll throw and exception!");
throw new RuntimeException("Just kidding!");
}

public static void throwException2() {
System.out.println("I'll throw an exception!");
try {
throw new RuntimeException("Just kidding!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

public static void throwError1() {
System.out.println("I'll throw an error!");
throw new OutOfMemoryError("Just kidding!");
}

public static void throwError2() {
System.out.println("I'll throw an error!");
try {
throw new OutOfMemoryError("Just kidding!");
} catch (OutOfMemoryError error) {
System.out.println(error.getMessage());
}
}
}

Комментарии

Популярные сообщения из этого блога

IoC:ApplicationContext, BeanFactory. Bean

Lesson1: JDK, JVM, JRE

Lesson_2: Operations in Java