try{
read the data
load file
}
catch(FileNotFoundException ex){
use local file and continue
}
try{ // Risky code
open db connection
read data
}
catch(Exception e){ // Handling code
sout("file not found");
}
finally{ // Cleanup code
close db connection
}
Class Test{
psvm(String[] args){
PrintWriter pw = new PrintWriter("abc.txt");
pw.print("Hello")
}
}
Throwable is partially checked Exception.
e.printStackTrace() -> o/p: starts with name of Exception: Description & stackTrace.e.toString()
sout(e);
sout(e.toString) -> o/p: starts with name of Exception: only Description no stackTracee.getMessage() -> o/p: only description no stackTrace. No name of exception and stackTrace.try{
//code}
catch(IOException|SQLException ex){
ex.printStackTrace();
}
BufferReader br = null;
try{
br = new BR((new FR("input.txt"));
// based on our requirement use 'br' to read data
}
catch(IOException e)
{
// Handling code
}
fianlly{
if(br != null)
{
br.close();
}
}
try(BR br = new BR(new FR("input.txt")))
{
use br based on our requirement. once control reaches end of try block automatically br will be closed. we are not requires to close explicitly.
}
catch(IOException e)
{
// handling code
}
class Eat{
void method()throws IOException{
throw new IOException("device error");
}
}
class Testthrows{
public static void main(String args[])throws IOException{ //declare exception
Eat eat=new Eat();
eat.method();
System.out.println("normal flow...");
}
}
class Testthrows{
void eat()throws IOException{
throw new IOException("device error"); //checked exception
}
void food()throws IOException{
eat();
}
void goodFood(){
try{
food();
}catch(Exception e){System.out.println("exception handled");}
}
public static void main(String args[]){
Testthrows obj=new Testthrows();
obj.goodFood();
System.out.println("normal flow...");
}
}