1public class MyClass {
2 public static void main(String[ ] args) {
3 try {
4 int[] myNumbers = {1, 2, 3, 4, 5, 6};
5 System.out.println(myNumbers[10]);
6 } catch (Exception e) {
7 System.out.println("Something went wrong. check again");
8 }
9 }
10}
11
1First try block try to handle it
2if not then catch block will handle it.
3Finally block will executed regardless
4of the outcome
1async function promHandler<T>(
2 prom: Promise<T>
3): Promise<[T | null, any]> {
4 try {
5 return [await prom, null];
6 } catch (error) {
7 return [null, error];
8 }
9}
10
1try {
2 try_statements
3}
4catch (exception_var) {
5 catch_statements
6}
7finally {
8 finally_statements
9}
10
1try
2 {
3 MailMessage mail = new MailMessage();
4 SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
5
6 mail.From = new MailAddress("your_email_address@gmail.com");
7 mail.To.Add("to_address");
8 mail.Subject = "Test Mail";
9 mail.Body = "This is for testing SMTP mail from GMAIL";
10
11 SmtpServer.Port = 587;
12 SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
13 SmtpServer.EnableSsl = true;
14
15 SmtpServer.Send(mail);
16 MessageBox.Show("mail Send");
17 }
18 catch (Exception ex)
19 {
20 MessageBox.Show(ex.ToString());
21 }
1const input = require('readline-sync');
2
3let animals = [{name: 'cat'}, {name: 'dog'}];
4let index = Number(input.question("Enter index of animal:"));
5
6try {
7 console.log('animal at index:', animals[index].name);
8} catch(err) {
9 console.log("We caught a TypeError, but our program continues to run!");
10 console.log("You tried to access an animal at index:", index);
11}
12
13console.log("the code goes on...");
14