🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

java UNIT 3 (1).pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

UNIT 3 Java Exception Handling What is Exception in Java? Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. Hierarchy of Java Exception classes T...

UNIT 3 Java Exception Handling What is Exception in Java? Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. Hierarchy of Java Exception classes Types of Java Exceptions Checked Exception Unchecked Exception Error Checked Exception The classes that directly inherit the Throwable class except RuntimeException and Error are known as checked exceptions. For example, IOException, SQLException, etc. Checked exceptions are checked at compile- time. Unchecked Exception The classes that inherit the RuntimeException are known as unchecked exceptions. For example, ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime. Error Error is irrecoverable. Some example of errors are OutOfMemoryError, VirtualMachineError, AssertionError etc. Java Exception Keywords Keyword Description try The "try" keyword is used to specify a block where we should place an exception code. It means we can't use try block alone. The try block must be followed by either catch or finally. catch The "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later. finally The "finally" block is used to execute the necessary code of the program. It is executed whether an exception is handled or not. throw The "throw" keyword is used to throw an exception. throws The "throws" keyword is used to declare exceptions. It specifies that there may occur an exception in the method. It doesn't throw an exception. It is always used with method signature. Example public class JavaExceptionExample{ public static void main(String args[]){ try{ //code that may raise exception int data=100/0; }catch(ArithmeticException e){System.out.println(e);} //rest code of the program System.out.println("rest of the code..."); } } Example class TestFinallyBlock { public static void main(String args[]){ try{ //below code do not throw any exception int data=25/5; System.out.println(data); } //catch won't be executed catch(NullPointerException e){ System.out.println(e); } //executed regardless of exception occurred or not finally { System.out.println("finally block is always executed"); } System.out.println("rest of phe code..."); } public class TestThrow1 { //function to check if person is eligible to vote or not public static void validate(int age) { if(age

Use Quizgecko on...
Browser
Browser