Showing posts with label exception handling. Show all posts
Showing posts with label exception handling. Show all posts

Aug 9, 2020

Exception Handling in Java | JAVA Langauge | Coding Winds

 

EXCEPTION  HANDLING IN JAVA

 

Hey guys, we are back with another blog of java and today we are gonna talk about exception handling in java. Exception handling refers to pre-handling the runtime errors in the code and predefining the action to implement if that exception occurs in the code. Java provide robust and object oriented way to provide handle exception scenarios known as java exception handling. Let’s get into examples to understand it better.

Ex:

public class Coding_Winds {    

public static void main(String args[]) {

         int i, j=2, k=0;

         i=j/k;

         System.out.println(i);

}   

     }

For the above code , if we will run it we will get a runtime error and we will get the following message on the console

Exception in thread "main" java.lang.ArithmeticException: / by zero

     at solution.Student.main(Coding_Winds.java:10)

 

Now if we will handle this following exception in the code, then we can deal with it in an specified way. So for the above  case if we use the exception handling. Below is the code after the implementation of exception handling.

public class Coding_Winds {

public static void main(String args[]) {

     try {

         int i, j=2, k=0;

     i=j/k;

     } catch(ArithmeticException e) {

         System.out.println(e.getMessage()+" occurred");

     }

}   

     }

Now to implement exception handling, we enclose the code that is likely to through an exception within curly braces and before opening curly braces we use the try keyword and the catch keyword after the ending braces. And inside the parenthesis after  the catch keyword we can put the type of error that we likely to get. In the above case we are using an arithmetic operator so we are likely to get an arithmetic error. And after mentioning the type of error we have to mention an object ‘e’ , and we can use this to call few methods we have. And  after this we can again put the set of code within the curly braces, in which we can call some methods, if any error occurs during runtime. For ex in this case the method ‘.getMessage()’ returns the problem which is causing the error.

For the above code , we will receive the following message on the console

/ by zero occurred

 

Now if you want to execute the code, below the code which you think may throw error at runtime, then you can execute it.

 For ex:

public static void main(String args[]) {

     try {

         int i, j=2, k=0;

     i=j/k;

     } catch(ArithmeticException e) {

         System.out.println(e.getMessage()+" occurred");

     }

     System.out.println("Some important code");

}   

     }

Now the output  on the console for the following code is


/ by zero occurred

Some important code

 

And if you wouldn’t have used the exception handling code for the above code then the compiler wouldn’t have reached down to the part , after the code which throws exception.

For example in the below code ,the line saying “Some important code”  will not get printed.

public class Coding_Winds {    

public static void main(String args[]) {

         int i, j=2, k=0;

     i=j/k;

     System.out.println("Some important code");

}   

     }

 

Now on running this code, there will be only the following message on the compiler


Exception in thread "main" java.lang.ArithmeticException: / by zero

     at solution.Student.main(Coding_Winds.java:9)

 

Do let us know your reviews in the comment section below and we will be discussing more on exception handling in the blogs that will be coming up in future.

Jul 15, 2020

Exception Handling | Try | Except | Else | Finally | Coding Winds

EXCEPTION HANDLING

Hello guys, we here bring another blog which will help us in our competitive programming the most.

The topic is, “exception handling”. Exceptions in python are objects that represent errors; they can be raised by passing invalid arguments to function or performing certain illegal operations or by explicitly.

It actually returns back the information about the error and how it was raised.

A basic example,

Output

We handle the exception using 4 statements, they go as:

a)     Try: This statement lets you test a code for errors.

b)     Except: This statement lets you handle the errors. This block is required even if the code passes

c)      Else: This statement will work if the code doesn’t have any error.

d)     Finally: This statement will be executed regardless of the presence of errors

Take a look at another example,

Output

Exception handling is very important when it comes to files in python.

Check this out,


Raise an Exception

As a programmer we can choose to throw an exception if conditions occur.

The keyword used is, raise.

Look at the example,

Output

Similarly we can define what type of error we can throw.

Look at the code below,

Output

 

Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.

Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


Do subscribe to our daily blog update by clicking here.


Thank You!