Lesson: Exception

1. Problem:

package az.etibarli;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a / b);
System.out.println("Continue...");
}
}

 -- Solution:

package az.etibarli;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
try {
System.out.println(a / b);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Continue...");
}
}


Demeli try-catch bloku iwletmedikde JVM exceptionu tutacaq printStackTrace() metodunu cagiracaq ve

sistemden cixacaq.


-- eslinde printStackTrace nece iwleyir:

package az.etibarli;

public class Main {
public static void main(String[] args) {
try {
System.out.println(3 / 0);
} catch (Exception e) {
StackTraceElement[] elements = e.getStackTrace();
System.err.println(e.getClass().getName() + ":" + e.getMessage());
for (int i = 0; i < elements.length; i++) {
StackTraceElement el = elements[i];
System.err.println("\tat " + el.getMethodName() + "(" + el.getFileName() + ":" + el.getLineNumber() + ")");
}
}
}
}


-- Oz exception class imizi yaratmaq:

*** An Exception is checked and RuntimeException is unchecked

package az.etibarli;

public class MyException extends Exception {

public MyException(String message) {
super(message);
}
}
package az.etibarli;

public class Main {
public static void main(String[] args) {
String s = null;
if (s == null) {
try {
throw new MyException("Xeta...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}



--


package az.etibarli;

public class MyException extends RuntimeException {

public MyException(String message) {
super(message);
}
}
package az.etibarli;

public class Main {
public static void main(String[] args) {
String s = null;
if (s == null) {
throw new MyException("Xeta...");
}
}
}


-- Multiple catch:

package az.etibarli;

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

try {
String s = null;
System.out.println(s.length());
} catch (ArithmeticException e) {
System.out.println("Mathematical exception...");
} catch (NullPointerException e) {
System.out.println("Null pointer exception...");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array out of bounds exception...");
} catch (Exception e) {
System.out.println("Another type exception...");
}
}
}

Sonuncu catch blokunu ona gore yaziriq ki, yuxaridaki hec bir catch blokuna duwmese en azindan 

sonuncu bloka duwsun.


-- Yuxaridaki kodu qisaldaq:

package az.etibarli;

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

try {
String s = null;
System.out.println(s.length());
} catch (ArithmeticException | NullPointerException | ArrayIndexOutOfBoundsException e) {
System.out.println("Mathematical exception...");
} catch (Exception e) {
System.out.println("Another exception happened...");
}
}
}



-- Hem Exception-u hem de Error-u tutmaq ucun case:

package org.lesson7;

public class Main {
public static void main(String[] args) {
try {
System.out.println(5 / 0);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}





-- throws

package org.example4;

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

try {
throw new MyException("MyException...");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Continue...");
}
}

--

package org.example4;

public class MyException extends RuntimeException {

public MyException(String message) {
super(message);
}
}
package org.example4;

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

throw new MyException("MyException...");
System.out.println("Continue...");
}
}



-- Unchecked exception: Yoxlamaq mecbur deyil.

package az.etibarli;

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

public static void method() {
throw new ArithmeticException();
}
}


-- Checked exception: RuntimeException - dan extend etmir. Mecburuq yoxlamaga!

package az.etibarli;

import java.sql.SQLException;

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

public static void method() {
try {
throw new SQLException();
} catch (Exception e) {
e.printStackTrace();
}
}
}


package az.etibarli;

import java.sql.SQLException;

public class Main {
public static void main(String[] args) throws SQLException{
method();
System.out.println("Continue");
}

public static void method() throws SQLException{
throw new SQLException();
}
}















Комментарии

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

IoC:ApplicationContext, BeanFactory. Bean

Lesson1: JDK, JVM, JRE

Lesson_2: Operations in Java