Session 4 Try with Resources and Custom Exception.pptx

trainingdecorpo 10 views 11 slides Aug 08, 2024
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

nanana nana


Slide Content

T ry with Resources and Custom Exceptions ICT Academy 1

Try with Resources The try-with-resources statement is a try statement that declares one or more resources. The resource is as an object that must be closed after finishing the program. The try-with-resources statement ensures that each resource is closed at the end of the statement execution. You can pass any object that implements java.lang.AutoCloseable , which includes all objects which implement java.io.Closeable . The following example writes a string into a file. It uses an instance of FileOutputStream to write data into the file. FileOutputStream is a resource that must be closed after the program is finished with it. So, in this example, closing of resource is done by itself try. ICT Academy 2

Try-with-resources : import java.io.FileOutputStream ; public class TryWithResources { public static void main(String args []){ try( FileOutputStream fileOutputStream = newFileOutputStream ("/java7-new-features/ src /abc.txt")) { String msg = “WELCOME BACK!"; byte byteArray [] = msg.getBytes (); fileOutputStream.write ( byteArray ); System.out.println ("Message written to file successfuly !"); } catch(Exception exception){ System.out.println (exception); } } } ICT Academy 3

Try-with-resources Example : Using Multiple Resources import java.io.DataInputStream ; import java.io.FileInputStream ; import java.io.FileOutputStream ; import java.io.InputStream ; public class TryWithResources { public static void main(String args []){ try( FileOutputStream fileOutputStream =new FileOutputStream ("/java7-new-features/ src /abc.txt"); InputStream input = new FileInputStream ("/java7-new-features/ src /abc.txt")){ String msg = "Welcome to javaTpoint !"; byte byteArray [] = msg.getBytes (); fileOutputStream.write ( byteArray ); System.out.println ("------------Data written into file--------------"); System.out.println ( msg ); DataInputStream inst = new DataInputStream (input); ICT Academy 4

int data = input.available (); byte[] byteArray2 = new byte[data]; inst.read (byteArray2); String str = new String(byteArray2); System.out.println ("------------Data read from file--------------"); System.out.println (str);}catch(Exception exception){ System.out.println (exception); } } } ICT Academy 5

Try-with-resources Example: using finally block import java.io.FileOutputStream ; public class TryWithResources { public static void main(String args []){ try( FileOutputStream fileOutputStream = new FileOutputStream ("/home/ irfan / scala -workspace/java7-new-features/ src /abc.txt")){ String msg = "Welcome to javaTpoint !"; byte byteArray [] = msg.getBytes (); fileOutputStream.write ( byteArray ); System.out.println ("Data written successfully!"); }catch(Exception exception){ System.out.println (exception); } finally{ System.out.println ("Finally executes after closing of declared resources."); } } } ICT Academy 6

Custom Exception We can create our own exceptions that are derived classes of the Exception class. Creating our own Exception is known as custom exception or user-defined exception. Basically, Java custom exceptions are used to customize the exception according to user need. ICT Academy 7

Following are few of the reasons to use custom exceptions: To catch and provide specific treatment to a subset of existing Java exceptions. Business logic exceptions: These are the exceptions related to business logic and workflow. It is useful for the application users or the developers to understand the exact problem. ICT Academy 8

class InvalidAgeException extends Exception { public InvalidAgeException (String str) { super(str); } } public class TestCustomException1 { static void validate (int age) throws InvalidAgeException { if(age < 18){ throw new InvalidAgeException ("age is not valid to vote"); } ICT Academy 9

else { System.out.println ("welcome to vote"); } } public static void main(String args []) { try { validate(13); } catch ( InvalidAgeException ex) { System.out.println ("Caught the exception"); System.out.println ("Exception occured : " + ex); } System.out.println ("rest of the code..."); } } ICT Academy 10

class MyCustomException extends Exception { } public class TestCustomException2 { public static void main(String args []) { try { throw new MyCustomException (); } catch ( MyCustomException ex) { System.out.println ("Caught the exception"); System.out.println ( ex.getMessage ()); } System.out.println ("rest of the code..."); } } ICT Academy 11
Tags