wellnwill Marathahalli Bangalore

Try with Resource

class Student implements AutoCloseable
{
int id;
String name;

Student(int id,String name)
{
this.id=id;
this.name=name;
}

public void close()
{
System.out.println("id:"+id+" name:"+name);
}
}



public class Test2
{
public static void main(String args[])
{
System.out.println(1+1);
try(Student s1=new Student(1,"Neeraj");
Student s2=new Student(2,"Abhay"))
{
System.out.println("inside try");
}
catch(Throwable e)
{
System.out.println(e.getMessage());
}
}
}
//O/P-2, inside try, 
  //id:2 name: Neeraj, id:1 name:Abhay

class Student {
int id;
String name;
Student(int id,String name)
{
this.id=id;
this.name=name;
}
public void close()
{
System.out.println("id:"+id+" name:"+name);
}
}



public class Test2
{
public static void main(String args[])
{
System.out.println(1+1);
try(Student s1=new Student(1,"Dheeraj"))
{
System.out.println("inside try");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}


method description
e.printStackTrace() Name of Exception: message: stack trace
e.toString() Name of Exception: message
e.getMessage() Message

Default exception handler uses printStackTrace() method to print the message and stack tracing.

class Test1
{
public static void main(String args[])
{
try{
System.out.println(10/0);
}
catch(Throwable e)
{
e.printStackTrace();
System.out.println(e.toString());
System.out.println(e.getMessage());
}
}
}
Footer with Map