Exception Handling PDF
Document Details
Tags
Summary
This document provides an overview of exception handling in Java. It includes practical examples of "try" and "catch" blocks, explaining how to handle various types of exceptions, such as arithmetic errors and file not found exceptions. The document also discusses how to use these blocks in programs to create better fault tolerance.
Full Transcript
Exception Handling Scanner scan= new Scanner(System.in); int marks; System.out.println(“Enter marks:”) marks = scan.nextInt(); System.out.println(“Please enter marks as int”); boolean flag = true; Scanner scan= new Scanner(System.in); int marks; while(flag){ flag = false; try {...
Exception Handling Scanner scan= new Scanner(System.in); int marks; System.out.println(“Enter marks:”) marks = scan.nextInt(); System.out.println(“Please enter marks as int”); boolean flag = true; Scanner scan= new Scanner(System.in); int marks; while(flag){ flag = false; try { System.out.println(“Enter marks:”); marks = scan.nextInt(); } catch(Exception e){ System.out.println(“Please enter marks as int”); flag = true; } } Line No of Code – Multiple line try 1 2 3 try{ 4 5 6} catch(…){ 7 8} 9 10 File file = new File("not_existing_file.txt"); FileInputStream stream = new FileInputStream(file); File file = new File("not_existing_file.txt"); try { FileInputStream stream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); }