Skip to main content

Error Handling in try catch statement

Error Handling in try catch statement,
 try
    {
        b=0;
        c=a/b;
    }
 catch (Exception ex)
{} 
catch // related exception{} finally{} which exception will catch first statement?

try
{
int b = 0;
int c = 10 / b;
}
catch (Exception e)
{
}
catch(DivideByZeroException ae)
{
}
finally
{
}

Ans:-
Numerous catch blocks are evaluated from top to bottom for each exception that is thrown, but only one catch block is actually executed. The order in which the catch blocks are inserted is important because.NET will jump to the first catch block that is polymorphically consistent with the thrown exception. Catch blocks for exception classes that are the most specialized and descending should come first.

In your example, the exception's type—which acts as the starting point for all exceptions and is, therefore, polymorphically consistent with every exception thrown—is the initial catch.

The finally block is also always executed after the try and catch blocks. A finally block is always run, regardless of whether an exception is raised or a catch block matching the exception type is found.

Comments

Popular posts from this blog

OOP Concept with Real Time Examples

A design philosophy is OOP. Object Oriented Programming is what it stands for. In contrast to outdated procedural programming languages, object-oriented programming (OOP) employs a separate set of programming languages. In OOP, everything is categorised as self-sustaining "objects". As a result, you achieve re-usability using the four core concepts of object-oriented programming. Programmes are organised around objects and data rather than action and logic in the object-oriented programming (OOP) paradigm.    Let's use your "Leg" as an example to grasp the object orientation clearly. The class "Leg" is one. Left and right legs are objects of type Leg on your body. A series of electrical impulses supplied through your body parts (through an interface) are what manage or control their primary functions. As a result, the body part serves as an interface between your body and your legs. The Leg is a well-designed class. The attributes of the Leg are m...

ASP.NET MVC Questions and Answers

Model View Controller (MVC) ·  Model ·  View ·  Controller Model(Business Layer): It is a business entity for representing the application data.  Assembly used to define MVC framework Advantages ·  Provides a clean separation of UI, Model and Controller. Clear separation of application concerns (Presentation and Business Logic). It reduces complexity that makes it ideal for large scale applications where multiple teams are working. ·  Easy to Unit test. ·  Re-usability of Model and View. We can have multiple models can point to same view and vice versa. ·  Improved code structure. ·  Separation of Concerns (Process of breaking programs into various units)    ·  Filters:  The ASP.NET Web API uses most of built-in filters from MVC ·  Model Binding & Validation: ASP.NET Web API uses same model binding functionality, but HTTP specific context related operations only. ·  Routing...

The key to achieving long-term success is a never-ending commitment to learning

Success is a personal, complex idea that differs from person to person. It includes a sense of accomplishment, fulfilment, and personal development in addition to simple monetary or material gains. Success is the accomplishment of one's objectives, aspirations, or desired results, which are frequently consistent with one's morals and life's mission. It entails moving forward, conquering challenges, and consistently aiming to do better. Success can be found in many areas of life, such as work, relationships, self-improvement, health, and societal contribution. Success is ultimately a highly individualized endeavor that represents each person's particular goals and ideals. People can better traverse the constantly shifting landscape of technologies, market trends, and industry demands by obtaining new information and skills. It improves problem-solving skills, fosters adaptation, and creates possibilities for growth, all of which are important for both personal and profes...